Hi everybody,

I’m just trying to get my space craft controls working properly. So far I can move it forward and back, and rotate it on all 3 axis. I’ve had problems with getting it to move and rotate smoothly, but its all gone now - rotation and movement is nice and smooth.

The problem is that rotating the craft does nothing to the direction of its movement - it still moves in global coordinates, regardless of its orientation.

This is a chunk of code, which I’m using for moving the object forward:

``````if (Input.GetKey(".")) // Forward thrust
{
fSpeed = fSpeed + 2.0f;
vect.z = fSpeed * 10 * Time.deltaTime;

audio.volume = MasterScript.fSoundVolume;
}

//actual rotation
transform.Rotate(Vector3.right * fVRotation * Time.deltaTime);
transform.Rotate(Vector3.forward * fHRotation * Time.deltaTime);

//translation
rigidbody.velocity = vect;
``````

Now I understand (I think) that in order to move the object in the desired direction and speed, I have to change the vect Vector3 to point to the desired position in 3d space with a specific magnitude, based on the rotation and speed of the object… and this is the part that I don’t understand.

what confuses me even more is the fact that when I say

``````vect.
``````

Intellisense shows 3 attributes - x, y and z

but when I say

``````Vector3.
``````

Intellisense shows many more attributes like forward, right, left, up, down, etc

I don’t get that since vect variable is of type Vector3, so it should have the same properties?

…I’m really confused. I’ve tried reading some stuff on the net, but it didn’t really help me understand. Is there an easy way to solve this, or am I doing the whole thing wrong?

oh yeh, and in case this will help in understanding my code, this is the method of gradually reducing speed (I have similar ones for speed and rotation alike)

``````    void dampenSpeed()
{
if (fSpeed < -0.1f) //if moving backwards, increase the speed
{
fSpeed = fSpeed + (fSpeed * 0.2f * -fDampeningFactor); //Modify - not sure about this... should work without a minus?
//Debug.Log("Increasing Speed");
}
else if (fSpeed > 0.1f) //if moving forward, decrease the speed
{
fSpeed = fSpeed - (fSpeed * 0.2f * fDampeningFactor);
//Debug.Log("Decreasing Speed");
}
}
``````

There’s nothing in your code about moving your ship in the direction its facing. All I see is

``````vect.z = fSpeed * 10 * Time.deltaTime;
``````

and

``````rigidbody.velocity = vect;
``````

No matter how you rotate your ship you move your ship on the world space z axis. That axis never changes so it will always move in that direction. You need to move you ship along its z axis in local space. For that you can use this: Unity - Scripting API: Transform.TransformDirection.

Vector3.up, left right etc is just shorthands for writing Vector3(1, 0, 0) etc.

EDIT: You can visualize this if you while playing your game look in your scene view and select your ship then switch the transform gizmo between local space and world space and you’ll notice that the local space axis rotate with your ship while the world space does not. As it is now you move your ship along the world space axis which of course is not what you want.

vect is an instance of the Vector3 class.

Vector3 has a bunch of class properties like up, down etc that you have seen. These are static variables that are available via the class reference Vector3. vect doesn’t have these because it is an instance, it has a different set of properties which are relevant to an instance of the Vector3 class.

To move in the gameobjects local forward direction just use something like

rigidbody.velocity = transform.forward * fSpeed * Time.deltaTime;

Thank you SO MUCH!!

I’ve spend hours and hours trying to figure this out!! It’s working now - I can fly around in space!!!

Oh and as far as I understand,

``````rigidbody.velocity = transform.forward * fSpeed * Time.deltaTime;
``````

is the same as

``````rigidbody.velocity = transform.TransformDirection(vect);
``````

right? I mean it produces the same results…

oh and once again, thank you!!

Except that the first line multiplies with fSpeed and Time.deltaTime and the second doesn’t, their directions are indeed identical.