instantiate rotation

Hey Guys,

i am trying to instantiate a rocket, which works fine:

Instantiate(ammo, this.gameObject.transform.FindChild("ShootArea").transform.position, this.gameObject.transform.rotation);

I wanna instantiate it from “ShootArea”, which is attached to a rotating turret, the turret can only rotate
on y axis, I want to have the rockets back looking to the turret and the rockets front to the target, but I dunno why
it is spawned with a wrong rotation, can anyone help me with this?

Cheers

Hello there!

To calculate a rotation from one point to another you can simply do the following:

Vector3 direction = targetPosition - startPosition;
Quaternion rotation = Quaternion.LookRotation(direction);

Now what is worth noting is that your object should be oriented around the Z-axis for this rotation to be correct.
So if your missile for example is a cube scaled (1, 3, 1) it would be oriented around the Z-axis and result in an incorrect rotation.
If you were to change said cube to (1, 1, 3) instead the result should be correct.

Thanks Myx,

I will try that out tonight.

Just for anyone else, it worked fine, already before I have created this post, for some wierd reason the rotation of the rocket projectile was f**ked :wink:

Cheers

The trick to positioning a bullet is quite simple. Take a bullet, put it the point of the barrel you first want it to come out. Remove it’s collider and turn its rendering off. This is now a perfect position of where you would instantiate a new bullet from.

var bullet  : Transform;
var bulletPoint : Transform;
var bulletSpeed = 500.0;

var shot = Instantiate(bullet, bulletPoint.position, bulletPoint.rotation);
shot.rigidbody.velocity=bullet.TransformDirection(Vector3.forward) * bulletSpeed;

or something like that.