- Home /
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
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 :)
//////////////////////////////////
//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));
}
}
Answer by sandeepsmartest · Nov 26, 2012 at 08:06 AM
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 !
Your answer
Follow this Question
Related Questions
2D metroid boost ball 0 Answers
Jumping with Rigidbody.AddForce not working? 1 Answer
Angle and Velocity in a Projectile 1 Answer