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
Endoken
On the missile, you need this in a script: transform.rotation = Quaternion.LookRotation(rigidbody.velocity);
Thanks for your response, I added this script and I linked to missile: function Update() { transform.rotation = Quaternion.LookRotation(rigidbody.velocity); } Did you mean this? Console tell me that "There is no 'Rigidbody' attached to the "bullet1(Clone)" game object, but a script is trying to access it." I have to do something else? Thanks Endoken
Your script need to go on the missile. And there has to be a Rigidbody. You are accessing the rigidbody on the missile when you set the velocity on line 4.
Your code looks fine. And this is on the missile correct? What is happening with this code? This code depends on the missile facing positive 'z' when the rotation is (0,0,0). Any time you use LookRotation() or LookAt(), the positive 'z' size of the object is considered front.
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);
}
}
Thanks for your support, I'll try this night ;) see you soon
Hi Kurotatsu, I tried to apply your code, but doesn't works on my scene, probably because I use sprite 2D? In attachment a quick image of what I would do! [32884-unity_lookrotation.jpg|32884] I'm using Unity 4.5.3 Thanks! Endoken
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!
On the missile, you need this in a script: transform.rotation = Quaternion.LookRotation(rigidbody.velocity);
– robertbuThanks for your response, I added this script and I linked to missile: function Update() { transform.rotation = Quaternion.LookRotation(rigidbody.velocity); } Did you mean this? Console tell me that "There is no 'Rigidbody' attached to the "bullet1(Clone)" game object, but a script is trying to access it." I have to do something else? Thanks Endoken
– EndokYour script need to go on the missile. And there has to be a Rigidbody. You are accessing the rigidbody on the missile when you set the velocity on line 4.
– robertbuMaybe I doing something wrong... var speed = 10; function Start () { rigidbody.velocity = transform.TransformDirection (new Vector2(speed, 0)); } function Update() { transform.rotation = Quaternion.LookRotation(rigidbody.velocity); }
– EndokYour code looks fine. And this is on the missile correct? What is happening with this code? This code depends on the missile facing positive 'z' when the rotation is (0,0,0). Any time you use LookRotation() or LookAt(), the positive 'z' size of the object is considered front.
– robertbu