- Home /
TransformDirection and Scale
I've made an empty game object that rotates with the keyboard input up to 80 and down to -80 eulers, and attached to it a gun item with a simple script.
Rigidbody2D Bullet = Instantiate(bulletPrefab,transform.position,transform.rotation) as Rigidbody2D;
Bullet.velocity = transform.TransformDirection(new Vector3(shootingSpeed,0,0));
**Since i'm just rescaling the whole character -1 when turning and TransformDirection doesn't get scaled, i get the effect in the picture: the velocity is set as if i didn't flip the object.*
I've tried by multiplying quaternions like this to no avail, most likely because i don't understand quaternions:
Quaternion bonus = Quaternion.Euler (0,0,180);
Quaternion newrotation = transform.rotation*bonus;
Rigidbody2D Bullet = Instantiate(bulletPrefab,transform.position,newrotation) as Rigidbody2D;
Any idea?
If you're trying to move the bullet forward with regards to the gun transform, just use transform.forward. See if this works:
Bullet.velocity = transform.forward * shootingSpeed;
This is not applying any force and the bullet is just falling down due to gravity.
Answer by static_cast · Dec 08, 2014 at 01:12 AM
Typically I would use Bullet.velocity = transform.rotation * shootingSpeed;
to set the velocity.
Otherwise you would use TransformDirection like this:
Bullet.velocity = transform.TransformDirection(Vector3.forward) * shootingSpeed;
You can't multiply a quaternion (transform.rotation) for a float (shootingSpeed).
And this is not applying any force, even if i set shootingSpeed for an extremely high amount. Bullet.velocity = transform.TransformDirection(Vector3.forward) * shootingSpeed;
Whoops sorry. This is what I meant to say:
Player.transform.forward * shootingSpeed;
I was referencing the wrong shooting script I had. ;)
$$anonymous$$ake sure Player
is your, well... player. :)
Player's transform.forward can't help me. The player itself is not rotating. Using anything but TransformDirection(new Vector3(shootingSpeed,0,0))
is not setting any velocity. Isn't there a way to create a new quaternion that stores the modified rotation?