- Home /
Unwanted jittery behavior
I have the following code in a script for a 2D game in the XY plane
function FixedUpdate () { // Make sure we are absolutely always in the 2D plane and with the right rotation. transform.position.z = 0; transform.eulerAngles= Vector3 (270,0,0); }
function LateUpdate () { var realWorldPosition: Vector3 = Camera.main.ScreenToWorldPoint (Input.mousePosition); var gamePosition: Vector3 = Vector3(realWorldPosition.x, realWorldPosition.y,0); transform.position = gamePosition; }
function OnCollisionEnter (collision : Collision) { var forceDirection:Vector3 = -1*collision.contacts[0].normal; collision.rigidbody.AddForce (forceDirection * pushPower, ForceMode.Impulse); }
The GameObject this is attached to moves with the mouse and applies a force to any collider it encounters. The thing is, once a collision occurs, the attached gameobject becomes all jittery for a while. It then goes back to normal, but it's annoying.
I've set infinite linear and rotational drags to its rigidbody, but it doesn't work.
I have no idea how to solve this.
Answer by oquendo · Apr 21, 2011 at 10:10 AM
Apparently, the problem had to do with the calculation of the object's position and the code controlling the camera's position. They were both inside the LateUpdate function, but in different scripts. I moved them both to the same script, calculated the camera's position and THEN the object's, and it seems to work.
Answer by Fervent · Apr 20, 2011 at 08:54 PM
You are mixing Physics with transforms. You need to attach this object via a spring joint or something to a parent object that is moved via the mouse to avoid wacky behavior. set the parent object to kinematic. That will allow you to have a RidgidBody on an object while controlling it via transforms.
Avoid any transform modifications to a body under physics control.
transform.position.z = 0; transform.eulerAngles= Vector3 (270,0,0);
this is bad and can be accomplished via constraints on the RidgidBody instead.
Does this object HAVE to have a RidgidBody component? Since you are moving it with the mouse it doesn't sound like you need a RidgidBody attached, just a collider to test for collisions, In fact it sounds like you really need the collider to be a trigger instead and use OnTriggerEnter.
O$$anonymous$$, that's useful, thanks. I'll check it out before giving a definite O$$anonymous$$.
If there's no rigidbody attached, can I still access the collision information? I would need to use the normal to calculate the direction for the applied force. Also, if I have no Rigidbody, then I guess I need to keep the transform.position and transform.eulerAngles statements. Is that right?
The jittery behavior seems unrelated to Physics, I removed the Rigidbody but the problem remains. I now think the problem is related to the lapse between the calculation of the object's position (var realWorldPosition: Vector3 = Camera.main.ScreenToWorldPoint (Input.mousePosition)) and the camera refreshment. Or something related. Any ideas?
The problem gets worse when the object is moving fast, which seems to confirm my comment above...
Your answer
![](https://koobas.hobune.stream/wayback/20220613124000im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Objects with colliders going through walls and each other. 3 Answers
Collision with drag object 2 Answers
Ignore collision at high velocity. 1 Answer
Trouble with Physics.IgnoreCollision 0 Answers
Big issue with colliders 1 Answer