Velocity based on rotation

My endeavour seemed simple enough, load an X-Wing and, using the mouselook script, I could have it flying around in no time at all!

The mouse look worked fine, even though I hadn’t got the ship moving yet, I knew it would be a huge problem.

Faced with the brevity of my ineptitude, I googled myself silly to no avail.

I realise there are minds in this world to which the answer is crystalline, in all its splendour, but it escapes me.

I wanted to make the ship move forward, according to its rotation, when I pushed W. So in the area where the quaternion is calculated and added to the transform:

Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
			Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.forward);
			
			transform.localRotation = originalRotation * xQuaternion * yQuaternion;
			if(Input.GetButton ("Horizontal"))
		{
		rigidbody.AddRelativeForce(transform.forward * inputThrottle * MAX_FORCE_MAGNITUDE); 	
		}

you can see I added some relative force. All this seems to do is make the model move in arbitrary directions.

I’m sure someone has encountered this, someone on these very forums, but believe me when I say I want to learn how to calculate the models velocity properly, and how to apply that to the model. I’m actually studying video games development at university!

If anyone could just make a quick post, or link me to one (I did search the forums, to no avail) to inform of the procedure holisticly, I would be eternally grateful to you.

Cheers.

I also had an issue when my object rotated and i wanted to move in a direction that he just goes anywere… The thing is that after you have updated the location, you will have to overwrite the originalRotation with the current rotation in every update.

i am currently at work but when i get home i will post my script to show you what i mean.

Well, if you want it to move a set speed in a certain direction without loss, just store a velocity magnitude you want and do this:

var moveSpeed : float = 20;

function Update()
{
   rigidbody.velocity = transform.forward * moveSpeed;
}

Thanks Gargerath, I think I’m missing a trick here, I always seem to get bizarre flight behaviour.

Just when I think I have it, I realise how poorly I understand.

It’s bewildering me, no end.

What I need is a functional, applied explanation on how to use Quaternions to achieve the following:

Make a model oscillate on yaw on a horizontal mouse axis.
Make a model shift pitch on a vertical mouse axis.
Make a model roll on delete, pgdown.
Strafe a model horizontally on a,d.
Strafe a model vertically on w,s.

I realise that information is trivial, but it may help to illuminate what I’m trying to achieve.

And others… cough. :smile:

OK… the last two are slightly easier. You don’t need quaternions to move in a given direction, you can just use transform.Translate. By default, this moves the object in the directions defined by its coordinate space, so if you say:-

transform.Translate(Vector3.right * distance);

…the object will move to its right. Vector3 has up, right and forward properties, but you can use -up to mean down, -right to mean left, etc.

Quaternions have a very useful method called AngleAxis which, as you might expect, rotates a certain angle around a certain axis. For pitch, roll and yaw, the axes are Vector3.right, Vector3.forward and Vector3.up, respectively:-

var q: Quaternion = Quaternion.AngleAxis(20.0, Vector3.up); // 20 degree yaw

You can also use transform.Rotate() to rotate based on your current rotation. transform.Rotate(0,0,5 * Time.deltaTime) would roll five degrees per second, transform.rotate(0,5 * Time.deltaTime,0) would yaw five degrees per second, and transform.Rotate(5 * Time.deltaTime,0,0) would pitch five degrees per second. It defaults to Space.Local, which means it does all the rotation based on its current rotation. The above plus the script I gave before should be sufficient.

As the guys above said, you can just use

transform.Translate(Vector3.forward);

if you don’t want physics.
If you do want physics, you can use

rigidbody.AddRelativeForce(Vector3.forward);

The difference between my code and your code is that you used ‘transform.forward’ rather than ‘Vector3.forward’.

How do you rotate the physics? So if I press right the physics turn right and forward is in a different direction.