Hello,
I have one problem.
I use ForceMode.Acceleration to get my ship flying around with acceleration, but I dont know how to stop it immediately when I stop pressing “w” key on keyboard or “s” key for reverseThrust. How can I stop my ship immediately?
Here is the code:
using UnityEngine;
using System.Collections;
public class MyShip : MonoBehaviour {
public string keyTurnLeft;
public string keyTurnRight;
public string keyThrust;
public string keyReverseThrust;
public string keyFire;
public float thrustTurnPower;
public float thrustPower;
void Update ()
{
if(Input.GetKey(keyTurnLeft))
{
rigidbody.AddTorque(Vector3.up * -thrustTurnPower, ForceMode.Acceleration);
}
else if(Input.GetKey(keyTurnRight))
{
rigidbody.AddTorque(Vector3.up * thrustTurnPower, ForceMode.Acceleration);
}
else
{
rigidbody.angularVelocity = Vector3.zero;
}
if(Input.GetKeyDown(keyThrust))
{
rigidbody.AddForce(transform.forward * thrustPower, ForceMode.Acceleration);
}
else if(Input.GetKey(keyReverseThrust))
{
rigidbody.AddForce(transform.forward * -thrustPower, ForceMode.Acceleration);
}
}
}