Hello Again! I’ve been having problems for a few days now. I have a missile that collides with a target. I want it to react differently depending on different types of missiles OnTrigger.
When I use this code, it works (The target get’s shoved in the direction the missile came from)
Vector3 obj_velocity;
Vector3 impact_velocity;
// Update is called once per frame
void Update ()
{
impact_velocity.x = Mathf.Lerp (impact_velocity.x, 0, .05f);
impact_velocity.y = Mathf.Lerp (impact_velocity.y, 0, .05f);
impact_velocity.z = Mathf.Lerp (impact_velocity.z, 0, .05f);
transform.Translate (obj_velocity + impact_velocity);
}
void OnTriggerEnter(Collider collision)
{
Debug.Log("BLAH Enter called." + collision.gameObject);
Vector3 direction = collision.gameObject.transform.position - transform.position;
impact_velocity = -direction / 4;
}
However, when I try to make it recognize if the object hitting it is equal to a particular public GameObject in its’ inspector. It doesn’t think the object hitting it is the same.
public GameObject my_missile_ref;
Vector3 obj_velocity;
Vector3 impact_velocity;
// Update is called once per frame
void Update ()
{
impact_velocity.x = Mathf.Lerp (impact_velocity.x, 0, .05f);
impact_velocity.y = Mathf.Lerp (impact_velocity.y, 0, .05f);
impact_velocity.z = Mathf.Lerp (impact_velocity.z, 0, .05f);
transform.Translate (obj_velocity + impact_velocity);
}
void OnTriggerEnter(Collider collision)
{
Debug.Log("BLAH Enter called." + collision.gameObject);
if (collision.gameObject == my_missile_ref)
{
Vector3 direction = collision.gameObject.transform.position - transform.position;
impact_velocity = -direction / 4;
}
}
my_missile_ref is the object that In instantiate to shoot at the target. It is also the same object that I drag into this script’s inspector for the ‘public GameObject my_missile_ref’ variable.
But when I say if (collision.gameObject == my_missile_ref) it never goes onto the things inside the if statement.
Sorry for the long post, I really appreciate the help guys-