- Home /
Control and restrict object movement
Hi I am trying to make a game where a player controls a plane and i want to have it restricted to move and face in only one direction. I also need to be able to rotate the plane (do barrel rolls).
The problem I am facing is that when ever I adjust the pitch of the plane while doing a barrel roll it no longer faces along the z axis
What I have tried is using the lookAt function and using a cube and moving it up and down to adjust the pitch of the plane which works but dose not allow me to use AddTorque to add spin the plane. I have also tried to adjust the pitch and spin just using AddTorque but I get the front of the plane no longer facing along the z axis.
I am using a Ridgidbody on the plane and have the rotation frozen on the x axis but this did not help. Im sure there is more code needed to get something like this working but this is what I have at the moment.
#pragma strict
var target: Transform;
private var rb: Rigidbody;
var speed: float;
var spin: float;
function Start()
{
rb = GetComponent.<Rigidbody>();
}
function FixedUpdate()
{
//pitches the nose down
if(Input.GetKey(KeyCode.UpArrow))
{
rb.AddTorque(Vector3.right * spin);
}
//pitches the nose up
if(Input.GetKey(KeyCode.DownArrow))
{
rb.AddTorque(Vector3.left * spin);
}
//barrel roll right
if(Input.GetKey(KeyCode.RightArrow))
{
rb.AddRelativeTorque(Vector3.forward * spin);
}
//barrel roll left
if(Input.GetKey(KeyCode.LeftArrow))
{
rb.AddRelativeTorque(-Vector3.forward * spin);
}
//add force in the direction its facing
rb.AddRelativeForce(transform.forward * speed);
}
Your answer
Follow this Question
Related Questions
Angles from quaternion/vector Problem. 2 Answers
Lookat function moves object backwards 0 Answers
Creating an offset for the LookAt() function 2 Answers
Rotate player to LookAt a touch position 2 Answers
Rotating object towards transform array 2 Answers