I have a problem with the shooting direction of the bullets.
The player is always facing toward to mouse with LookAt(mouseWorldPos)
and the: Instantiate(bulletPrefab, transform.position, transform.rotation);
The bullet have speed and in Update: Translate(Vector3.forward * speed * Time.deltaTime).
When the player dont move and just shoot its okay, the shooting direction is okay.
But, when the player is moving and shooting in the same time the shooting direction is wrong.
To be honest I don’t understand why it works at all. It’s a common misunderstanding that Vector3.forward is the forward direction of an object, but it’s actually the world forward direction, which is constant. If you call transform.forward on the bullet script, that will be the direction of the bullet should travel in.
So in your bullet code, use transform.foward instead of Vector3.forward, that should fix the problem.
You say that the player is always facing the mouse cursor, but are you sure that the movement code does not also change the direction that the player is facing? If the movement code you use changes the direction of the player, and if the bullet script executes after the player’s movement for the frame but before the player is oriented to face toward the mouse cursor for that frame, then that could do it.
In the situation I describe, the player might not face the cursor all the time, but if the part that makes the player face the mouse always happens after the movement for the frame before it gets rendered, it might always look as if the player is facing the mouse even if the player is facing some other direction while moving.
That’s the only thing I can think of. Check to make absolutely sure that there is nothing else changing the player’s direction.
Sorry, don’t know then. The only other thing I can suggest is to put in some debug code and/or step through the game in debug mode.
If you check to see what the transformations of everything are between each of those actions there, then maybe you can notice something that is not as it should be.
The problem is with the code update of your bullet. You ask it to move to the global z axis while ur player face to another direction. Vector3.forward is not local. It’s global. And global will never change. For example, global of z is north. If the bullet is facing to east, then the local z axis of bullet is east. However the code says go to the north, always north. As a result, it will get messed up like what you have.
One more thing I don’t suggest you make another script for moving. The reason above about transform.forward is because you instantiate the bullet. Sometimes it mess up the rotation of bullet as well which gives you the result It’s not working.
My solution is, give the bullet a rigidbody with useGravity off. In your instantiate script, use AddForce.
ex: var bullet = instantiate …
bullet.AddForce (transform.forward * 20);
1)I don’t want to use rigidbody because I wants max performance.
2)function Update() {
// Move the object forward along its z axis 1 unit/second.
transform.Translate(Vector3.forward * Time.deltaTime);
// Move the object upward in world space 1 unit/second.
transform.Translate(Vector3.up * Time.deltaTime, Space.World);
}
So you can see that I use the bullet’s axis and not the world’s axis.
3)When the player doesn’t move, the shooting direction is okay.