Rigidbody.position causes shaking

int x = Screen.width / 2;
int y = Screen.height / 2;
GameObject Cube = GameObject.Find(“Cube”);
Ray ray = Camera.main.ScreenPointToRay(new Vector3(x, y));
RaycastHit hit;

		if (Physics.Raycast(ray, out hit)) {
			Cube.rigidbody.position = hit.point; 
			Cube.rigidbody.velocity = new Vector3(0, 0, 0);
		}

Cube has rigidbody in it.

Where does this happen? In Update()? On a click? And what shakes - the Cube or something else? We need a bit more information before we can give any input on your problem.

Generally instead of setting the position of a rigidbody you want to use MovePosition. Maybe that will help?

The Cube and in Update();

VesuvianPrime: No.

1 Answer

1

I saw from your comments that you’re doing this in the Update method.

Interactions with the physics engine (rigibodies, etc.) should ONLY be done in the FixedUpdate method.

Also, according to the documentation for Rigidbody.position:

This is similar to setting transform.position, however the position will only be applied to the transform at the end of the physics step. If you want to continously move a rigidbody or kinematic rigidbody use MovePosition and MoveRotation instead.