MoveTowards() / RotateTowards() problem

Hi!

So my Enemy craft throwing missiles at Player craft. I used MoveTowards() to make the missile “Guided”, and it works. But the missile won’t rotate towards the Player craft, so it looks a little glitchy. I tried calling both MoveTowads() and RotateTowards(), but then the missiles are not firing.

Missle spawning and moving Code:

if (timer > waitingTime)
        {
            enemyProj = GameObject.Instantiate(missilePrefab, projectileSpawnPoint.transform.position + new Vector3(0,0,-10f), Quaternion.Euler(0, 180, 0));
            debrisDirection = new Vector3(Random.Range(-debDirOffsetx, debDirOffsetx), Random.Range(10, -debDirOffsety),
                Random.Range(100f, 200f));
            astroRB = enemyProj.GetComponent<Rigidbody>();
            astroRB.AddForce(debrisDirection * debrisSpeed, ForceMode.Impulse);

            txtMsg1.text = "";

            timer = 0;
        }

        enemyProj.transform.position = Vector3.MoveTowards(enemyProj.transform.position, craft.transform.position, Time.fixedDeltaTime * 50);
        //enemyProj.transform.position = Vector3.RotateTowards(enemyProj.transform.position, craft.transform.position, 1f, 0f);

What I am doing wrong? Thanks!

Current state:

Use Quaternion.LookRotation to build a rotation towards your target (the docs have an example), then feed that value to the second parameter of Quaternion.RotateTowards.

Your code is also mixing forces with manually moving the transform. You need to choose one or the other.

1 Like

Thanks, but RotateTowards() don’t accept Quaternion. Any thoughts?

Are you using Vector3.RotateTowards() or Quaternion.RotateTowards()? Because as @GroZZleR suggest you should be using the latter.

1 Like