I want to instantiate an object (a bullet) with a -Z direction, cause this object moves along this axis. But, I create this object in a script belong another object (the character) which rotate constantly in the time. The instantiated object has a rotation equal to the parent object.
I have two scripts:
The first one rotates the parent object (character) which instantiates the bullet.
function Update () {
transform.Rotate(0, 50 * Time.deltaTime, 0);
}
The second one instantiates the bullet when the character has a rotation
var v = Vector3(transform.position.x, 0, transform.position.z);
var instantiatedProjectile : Rigidbody = Instantiate(bullet, v, Quaternion.identity);
instantiatedProjectile.velocity = transform.TransformDirection(Vector3(0, 0, -bullet_speed));
Physics.IgnoreCollision(instantiatedProjectile.collider, transform.collider);
Actually, your problem is caused by transform.TransformDirection(…): it’s using the creator’s transform to convert a local direction to world space. You could simply eliminate it:
That transform means “me”, the shooter. Some reading says that TransformDirection changes from (the shooter’s) local space to world space. So that math is probably why bulletSpeed accounts for the player’s facing.
If you just want the velocity to be -Z, make it be -Z: velocity=Vector3(0,0,-bullet_speed);
The trick to adapting code is to look at each line; and figure out what it does, and whether that’s something you need.