Simple question about 3D Space and moving forward.

Alright, so I previously received some help regarding rotations that I greatly appreciated. What I am working on right now is just a practice 3D space shooter to learn controls. I have the ship that can change its rotation to point left and right when I hold those keys down, and I have set up space bar to be the thrust or forward motion. What I am trying to do is get the ship to move in the direction it is currently pointing. I thought this would be easy because I was under the assumption that when I rotated an object in 3D space that the z axis changed with it and so forth. I thought a simple transform.forward would work. I did not realize that the X Y Z remain the same and that only the model was rotated. I spent quite a while trying to figure this out but with no luck. I know there must be an easy way of doing this because many games make their character or object move in the direction the model is facing. I did look over the Space Shooter tutorial that was recommended by another user but that one seems to only have the ship going left right up and down in a top-down 2D fashion. Anyway, before I go crazy trying some weird math or something I wanted to ask if someone can please let me know how you are suppose to go about doing this. Thanks again everyone.

2 Answers

2

you should be able to achieve this with transform.forward, you could try transform.Translate() but I think you’ll run into the same problem.

transform.Translate()

forward = transform.TransformDirection(Vector3.forward) would produce the results I think you want.

–

I usually let transform move me like so:

float speed = 0.2f;

void Update()

{
    transform.position += transform.forward * speed;
}

That does the trick. So simple, but I didn't think of that. Thank you.

–

Let me get this answer straight.. so transform.forward is the z axis of the transform. you said you tried that in your question but it led to you moving along the global z axis. When you rotate the transform the z axis of the transform moves in relation to the rotation? because you changed the position (global space) of the transform along the local transform.forward it moved it in relation to the rotated z axis? Thanks

–