simple shooting problem

I have a cube with a child camera
I give a mouselookx on the cube
I give a mouselooky on the camera

then i do this:
GameObject test = Instantiate(bullet, camera.transform.position, camera.transform.rotation);
Debug.Log(camera.transform.rotation);
Debug.Log(test.transform.rotation);

the rotation checks out the same.
The issue is this: if they rotated the same and I move the bullet in the local Z axis. shouldn’t it go straight out of the camera???
The problem is that it does not the further away from the z direction the worse the deviation

this is my bullet code for reference:
transform.Translate(transform.forward * Time.deltaTime * 5);

anyone know why this is?

Could be if you are only pivoting the camera, but you are using the transform.forward of the object, then you’re not moving camera +Z but rather object +Z.

It’s so easy to keep the scene and the game window open simultaneously. Put this code somewhere in an Update script on the cube, then another one on the camera:

Debug.DrawRay( tranform.position, transform.forward, Color.yellow);

That will show you which way each object is pointing. You can use different colors for cube and for camera and watch in the scene as you play the game.

transform.Translate() defaults to local movement. The bullet should use Vector3.forward instead of transform.forward or add the second parameter Space.World. Here is the link to the docs for that function.

2 Likes

Yeah the translate thing had to be Vector3.forward, thanks.