homing missile (tracking missile) using rigidbody with if else statement

EDIT* EDITED FORMAT OF CODE TO BE NEAT

Objective - to have the homing missile reach the desired game object. This is a 2D game with game objects that only move along the X and Y axis.

I have a homing missile or tracking missile. It uses physics movement. I have approached the task in 2 different ways.Both approaches work to a certain extent. Both use if else statements.
*1 use AddForce to change the velocity
*2 use rigigbody.velocity to change the velocity

Both approaches have undesired results
*1 using AddForce makes the homing missile keep moving even after it has reached the destination; however it correctly moves the missile along both the X and Y axis

*2 using rigidbody.velocity only moves the the missile along the Y axis and then correctly stop once it reaches its final destination on the Y axis(the other game objects Y position). However, it DOES NOT update the position on the X axis. For some reason the missile does not move along X axis.

I have tried the following code on both the FixedUpdate() and Update()

Your time and energy is appreciated. Thank You :slight_smile:

//////////////////////////////////
//rigidbody.AddForce
////////////////////////////////
if (tempExtraBall.isExtraBallUsingHoming)
{

        if (tempExtraBall.rigidbody.position.x < this.rigidbody.position.x)
        {	
            tempExtraBall.rigidbody.AddForce(new Vector3(1, 0, 0), ForceMode.Impulse);
        }

         else if (tempExtraBall.rigidbody.position.x > this.rigidbody.position.x)
        {
            tempExtraBall.rigidbody.AddForce(new Vector3(-1, 0, 0),ForceMode.Impulse);
        }

         if (tempExtraBall.rigidbody.position.y > (this.rigidbody.position.y + 1))
        {
            tempExtraBall.rigidbody.AddForce((Vector3.down * 1), ForceMode.Impulse);
        }
         else if (tempExtraBall.rigidbody.position.y < (this.rigidbody.position.y + 1))
        {
          tempExtraBall.rigidbody.AddForce((Vector3.up * 1), ForceMode.Impulse);
        }

}

//////////////////////////////////
//rigidbody.velocity
////////////////////////////////

if (tempExtraBall.isExtraBallUsingHoming)
{

        if (tempExtraBall.rigidbody.position.x < this.rigidbody.position.x)    
        {      
           tempExtraBall.rigidbody.velocity = tempExtraBall.extraBallSpeed * (new Vector3(1,0,0));          
        }

         else if (tempExtraBall.rigidbody.position.x > this.rigidbody.position.x)
        {
           tempExtraBall.rigidbody.velocity = tempExtraBall.extraBallSpeed * (new Vector3(-1, 0, 0));
        }

         if (tempExtraBall.rigidbody.position.y > (this.rigidbody.position.y + 1))
        {                
             tempExtraBall.rigidbody.velocity = tempExtraBall.extraBallSpeed * (new Vector3(0, -1, 0));
        }
         else if (tempExtraBall.rigidbody.position.y < (this.rigidbody.position.y + 1))
        {
            tempExtraBall.rigidbody.velocity = tempExtraBall.extraBallSpeed * (new Vector3(0, 1, 0));
        }

}

Hi !

I guess applying a character controller component to the missile and moving it using Character.Move function would help ! You can give the Direction of the missile and the gameobject multiplied by your speed in the Move function !