- Home /
Question by
Fohristiwhirl · Sep 05, 2014 at 03:40 PM ·
c#rigidbody2dtorque
Problem with Rigidbody2D.angularVelocity
Hi, I have a sprite that the player controls. I move the sprite (with Rigidbody2D attached to its game object) using the rigidBody2D.velocity = someVector;
For reasons unknown to me at times the rigid body appears to receive a large amount of torque from somewhere. It starts spinning slowly coming to a stop. I do not change any variable other than the velocity nor do i call any function on the rigidBody. The physics system must be doing it. My question is why? Here is the code that moves a rigidbody.
public class CharacterMovement : MonoBehaviour {
private Rigidbody2D characterRigidBody;
public float speed;
public Vector2 inputDirection;
void Awake ()
{
characterRigidBody = (Rigidbody2D)GetComponent (typeof(Rigidbody2D));
}
// Update is called once per frame
void FixedUpdate ()
{
characterRigidBody.velocity = inputDirection * speed;
}
}
Here is the code that used the CharacterMovement class.
private CharacterMovement motor;
void Awake ()
{
motor = (CharacterMovement)GetComponent (typeof(CharacterMovement));
}
void Update ()
{
Vector2 moveDirection = new Vector2 (Input.GetAxis ("Horizontal"), Input.GetAxis ("Vertical"));
float speed = moveDirection.magnitude;
moveDirection.Normalize ();
if (moveDirection != Vector2.zero)
transform.rotation = Quaternion.FromToRotation(Vector3.right, new Vector3(moveDirection.x, moveDirection.y, 0));
motor.inputDirection = moveDirection;
}
I can't work out where this mysterious force comes from. Brushing up against another rigidbody such as an NPC reduces the force and brings the player to a standstill.
Comment