Hi,
I would like to shoot a missile, and I did this:
function Fire () {
var clone : Rigidbody;
clone = Instantiate(projectile, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection (new Vector2(speed, 0));
}
It works properly, but how can I do to rotate “clone” along its trajectory?
Thanks
function Fire () {
var clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
clone.rotation = Quaternion.LookRotation(clone.velocity);
clone.rigidbody.AddForce(transform.forward*speed);
}
Check here for a similar script I wrote and tested that works:
var speed = 5.0f;
var rotateSpeed:float ;
var sphere:GameObject;
var child:Transform;
var ballPrefab:Rigidbody;
var ballSpeed:float = 200;
var spawnPoint:Transform;
function Update () {
if(Input.GetKeyDown(KeyCode.Space)){
var ball = Instantiate(ballPrefab, spawnPoint.position, spawnPoint.rotation) as Rigidbody;
ball.rotation = Quaternion.LookRotation(ball.velocity);
ball.rigidbody.AddForce(transform.forward*ballSpeed);
}
}
Update: I solved it!
The scripts are all correct, but my sprite rotated on wrong axis, I created an Empty gameObject as a Father and then I have corrected the rotation, now it works!
Thanks at all for the support!