Advertisement

New to Game Dev, Stuck on Force Concept

Started by May 31, 2023 09:50 PM
2 comments, last by muhammad_hamid 1 year, 5 months ago

I m new to game dev, searching for a community to help, I was just creating jelly shift replica as practice, I m stuck on a concept, (if you don't know about , there's a cube which scales up and down on touching), so i scale it using transform.local scale and increment in the x and decrement in the y while pressing up arrow key and vice versa on pressing down arrow, alongside, i m moving the cube through constant velocity by rb.velocity = transform.forward * forwardspeed in fixed update function. scaling is in update function, Problem is that when i move forward the cube using rb.velocity, while scaling, it hovers up the ground, because it scales down on y axis from both ends and hovers, but when i move it using transform.translate, it sticks to the ground, even on scaling, it doesn't hover up, i have the gravity and all the things, but i don't know why this happens, following is the code, I m worried why i cant get these concepts, Please if anyone can help me.

Jelly Shift Game Link: https://play.unity.com/mg/other/webgl-builds-350432

void Update()

{

scaleCube();

}

private void FixedUpdate()

{

movingCube();

}

private void scaleCube() {

	float verticalInput = Input.GetAxis("Vertical");

	if (verticalInput > 0)

		verticalInput = 1;

	else if (verticalInput < 0)

		verticalInput = -1;

	float newScaleX = transform.localScale.x - verticalInput * scaleSpeed * 	Time.deltaTime;

	float newScaleY = transform.localScale.y + verticalInput * scaleSpeed * Time.deltaTime;

// Clamp the scale within the specified range

	newScaleX = Mathf.Clamp(newScaleX, minX, maxX);

	newScaleY = Mathf.Clamp(newScaleY, minY, maxY);

	transform.localScale = new Vector3(newScaleX, newScaleY, transform.localScale.z);

}

private void movingCube()

{ 
// transform.Translate(Vector3.forward * ForwardSpeed* Time.deltaTime);

	cubeRB.velocity = transform.forward * ForwardSpeed;

}

// Update is called once per frame

Physics engines don't usually like to have non-uniform scaling for non-kinematic objects. I played your game and didn't notice any problems, it seemed to behave the way I would expect, until I ran out of track. You need to provide an example where you show the wrong behavior compared with the expected behavior, to understand the problem better.

Advertisement

Actually i used the transform.translate in the WebGL version of game, I just wanted to check if force would work fine but it hovers the cube. Searched the whole internet, some of video says, that velocity ignores the gravity, rb.addforce will continue to add force to object even after we left the key press and stop the force by nature of physics like by drag force. And Moveposition is good in interpolate and for smooth frame transitions. On the unity doc, it was mentioned that we should use velocity where we need instant velocity change like in FPS to jump the player. But in my cube, which method is better practice to add constant force that do not effect by our scaling.?i Used add force but it was giving unexpected behaviour like sometimes while scaling, the force would increase or sometimes it remain same. Actually, Honestly speaking I m overwhelmed by these physics thing as I m not too good in physics. I tried Machine Learning but shifted to Game Development as a passion. Now I don't know if it was right decision or not. I usually needed a long time to grasp a single concept.

Hovering on scaling down Y axis when using velocity change

This topic is closed to new replies.

Advertisement