Hi
I was hoping someone could tell me how I would go about causing an object to fly in a new direction immediately. Currently, I’m adding force to the Rigidbody, but this means that even after turning the object, the previously accumulated force remains. I know I should cancel this force, but I’m not sure what the best way of doing so would be.
Also, I HAVE managed to eliminate the force of my rotation, so that the object only rotates so long as a player is holding down the appropriate button. However, I’m not sure that the way I did it - manually providing a Vector3 (0, 0, 0), is the best way to do so. Can someone tell me whether there’s a better way?
Thanks!
public float speed = 10f;
public float turnSpeed = 0.01f;
private Rigidbody rBody;
private float accel;
private float acceleration = 10f;
private float steer;
// Use this for initialization
void Start ()
{
rBody = GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update ()
{
accel = Input.GetAxis ("Accel");
steer = Input.GetAxis ("Steer");
}
void FixedUpdate ()
{
rBody.AddRelativeForce (0f, 0f, accel * acceleration);
if (rBody.velocity.magnitude > speed)
rBody.velocity = rBody.velocity.normalized * speed;
if (steer == 0)
rBody.angularVelocity = new Vector3 (0, 0, 0);
else
rBody.AddRelativeTorque (0f, steer * turnSpeed, 0f);
}