1
\$\begingroup\$

I have been trying this for two days with no success. I cant figure out where I'm missing the point. All the missiles are moving towards the position of the target but not following it. The position remains fixed and all the newly created missiles come to this point instead of following the target.

Here is the code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HomingMissile : MonoBehaviour
{
    public Transform target;
    private Rigidbody rb;
    public float rotationSpeed;
    public float speed;

    Quaternion rotateToTarget;
    Vector3 direction;

    private void Start()
    {
        //target = GameObject.FindGameObjectWithTag("Target");
        rb = GetComponent<Rigidbody>();
    }

    private void Update()
{
    //direction = (target.transform.position - transform.position).normalized;
    //float angle = Mathf.Atan2(direction.z, direction.x) * Mathf.Rad2Deg;
    //rotateToTarget = Quaternion.Slerp(transform.rotation, rotateToTarget, Time.deltaTime * rotationSpeed);
    //rb.velocity = new Vector3(direction.x * speed, direction.y * speed, direction.z * speed);

    Vector3 direction = (target.transform.position - transform.position).normalized;
    float angle = Mathf.Atan2(direction.z, direction.x) * Mathf.Rad2Deg;
    Quaternion rotateToTarget = Quaternion.Euler(0, angle, 0);
    transform.rotation = Quaternion.Slerp(transform.rotation, rotateToTarget, Time.deltaTime * rotationSpeed);
    rb.velocity = direction * speed;
}

}

I selected the target(transform) using the inspector. I'm using Unity and C# obviously you know that.

What Im trying to achieve is that the missile should follow the position of the target in real time. And i can add the destroy code for the missile myself. Note : Please don't tag this as a duplicate. It is not. The game is 2D where Y is always constant. Vertical axis is X and Horizontal axis is X. The objects are 3D. That's why I can't use rigidbody2D.

\$\endgroup\$
2
\$\begingroup\$

You seem to have a rotateToTarget field which isn't doing anything, and you are calculating angle and doing nothing with it. Also note that Mathf.Atan2 accepts arguments in the order (y,x). I'm not sure if x,z is correct or not in your case. You might try swapping them around. Finally, a PSA: You can multiply a vector by a scalar and get a scaled vector. For example, your assignment to rb.velocity can be made much simpler.

Maybe something like this?

Vector3 direction = (target.transform.position - transform.position).normalized;
float angle = Mathf.Atan2(direction.z, direction.x) * Mathf.Rad2Deg;
Quaternion rotateToTarget = Quaternion.Euler(0, angle, 0);
transform.rotation = Quaternion.Slerp(transform.rotation, rotateToTarget, Time.deltaTime * rotationSpeed);
rb.velocity = direction * speed;

Oh, and lastly, I would recommend looking into a different way of animating your rotation. Using Quaternion.Slerp is not ideal for this purpose. See this StackOverflow answer explaining why and offering an alternative.

\$\endgroup\$
2
  • \$\begingroup\$ Thanks for the quick response. But the problem persists. There is slight change of rotation of the missile though. \$\endgroup\$
    – Joe_Vj _95
    Jun 4 '18 at 5:05
  • \$\begingroup\$ Replaced with your code and modified. I uploaded it here youtu.be/onFCUZeojrE \$\endgroup\$
    – Joe_Vj _95
    Jun 4 '18 at 5:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.