Question by
NeonDv · Apr 13, 2016 at 07:49 PM ·
rotationquaternion
Homing missile rotation
so i want to create a script for a homing missile when the target rotate the missile also rotate but in realistic way i dont want it to reverse it rotation and follow the target i want more like this way on the picture
this the code that i used i tought if used slerp it well do the job but the missile behave like what i said above
public class EnemyMovment : MonoBehaviour {
public GameObject plane;
public float speed = 15f;
public float turningSpeed = 5f;
void Start () {
}
// Update is called once per frame
void Update () {
destroyPlane();
}
void destroyPlane()
{
Vector3 Target = plane.transform.position;
Vector3 distance = Target - transform.position;
var angle = Mathf.Atan2(distance.y, distance.x) * Mathf.Rad2Deg - 90;
transform.rotation = Quaternion.Slerp(transform.rotation,(Quaternion.AngleAxis(angle, Vector3.forward)),turningSpeed*Time.deltaTime);
transform.position = Vector3.MoveTowards(transform.position, Target, speed * Time.deltaTime);
}
}
screenshot-2016-04-13-20-13-25.jpeg
(189.5 kB)
Comment
Best Answer
Answer by Kazuva · Apr 14, 2016 at 12:09 AM
I would use transform.Translate to move the missile so it would move in the direction it's facing, below i set an example how it could be done
public GameObject plane;
public float speed = 15f;
public float turningSpeed = 5f;
void Start()
{
}
// Update is called once per frame
void Update()
{
destroyPlane();
}
void destroyPlane()
{
Vector3 Target = plane.transform.position;
Vector3 distance = Target - transform.position;
var angle = Mathf.Atan2(distance.y, distance.x) * Mathf.Rad2Deg - 90;
transform.rotation = Quaternion.Slerp(transform.rotation, (Quaternion.AngleAxis(angle, Vector3.forward)), turningSpeed * Time.deltaTime);
//the moving I made
transform.Translate(Vector3.up * speed * Time.deltaTime);
//old moving
//transform.position = Vector3.MoveTowards(transform.position, Target, speed * Time.deltaTime);
}
thank you the rocket now behave kinda like how i want it if you have any thing else to make the good better that well be great thank you again