Hello!
I am working on a unique racing game set inside anti-gravity tunnels. My player can yaw with the "horizontal axis" and pitch with the "vertical axis". The player accelerates in the direction their ship is facing with "left shift". All rotation on the z axis is locked at zero. Here are my problems, my player doesn't accelerate in the direction that the ship is facing, locking the RigidBody's rotation does not stop it from rotating on the z axis and I need my ship to slow down when "left shift" is released rather than it suddenly stopping. This is my first attempt at using AddForce for a controller because my attempts with using Transform usually end up with the player hitting a static object and moving in a random direction afterwords.
Attach this script to an object with a RigidBody and turn off gravity:
var speed : float;
var yawspeed : float;
var pitchspeed : float;
function FixedUpdate(){
if(Input.GetKeyDown(KeyCode.LeftShift))
rigidbody.AddForce(0,0,speed);
if(Input.GetKeyUp(KeyCode.LeftShift)) {
rigidbody.velocity = Vector3.zero;
rigidbody.angularVelocity = Vector3.zero;
}
transform.Rotate(0, Input.GetAxis("Horizontal")*yawspeed*Time.deltaTime, 0);
transform.Rotate(Input.GetAxis("Vertical")*pitchspeed*Time.deltaTime, 0, 0);
}
I'm struggling with this one, so any help will be extremely appreciated! :D
↧