- Home /
Destorying despite IF Statement
I'm new to Unity so this may sound quite trivial to others. Basically, I'm attempting to create a small mine object that moves towards the player when he is within range and then explodes. Through some research and modifying, I found some coding that allowed me to make the mine wait until the player is within range and then charge towards him. When the Mine detects the player, the mine has a 5 second timer before it explodes/destroyed. My main issue is that the mine will destroy regardless of the distance between the Player and the Mine. Below is coding I'm using.
var start: Transform;
var end: Transform;
var target : Transform; //the enemy's target
var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning
var enemydistance =3;
var enemy : Transform; //current transform data of this enemy
function Awake()
{
enemy = transform; //cache transform data for easy access/preformance
}
function Start()
{
target = GameObject.FindWithTag("Player").transform; //target the player
}
function Update ()
{
//Detect Distance between Player and Mine
if ( Vector3.Distance( enemy.position, (Vector3.Lerp(start.position, end.position,Time.time) )) < enemydistance ) {
//rotate to look at the player
enemy.rotation = Quaternion.Slerp(enemy.rotation,
Quaternion.LookRotation(target.position - enemy.position), rotationSpeed*Time.deltaTime);
//move towards the player
enemy.position += enemy.forward * moveSpeed * Time.deltaTime;
//Destroy Mine after 5 seconds
Destroy(gameObject,5) ;
}
}
Basically, its ignoring the If statement regarding the distance and just destroys it after 5 seconds. Where abouts did I stuff it up and what can I do to fix it?
What is that Lerp doing at line 20 ?!
if ( Vector3.Distance( enemy.position, target.position ) < enemydistance )
It was part of a script that I found. Originally, when I was creating this script, the $$anonymous$$e would move regardless of distance. As I was looking for ways to prevent that, I came across the line you pointed out to be wrong. Still, I ended up adding the Lerp coding and the $$anonymous$$e wouldn't move until I was within range started working so I left it as that. Anyway, you're line of code fixed this issue so thanks for the help.
Answer by sparkzbarca · Apr 18, 2013 at 07:02 AM
what your going to want to do is create the mine object and add a sphere collider to it.
in the editor you will see an option called is Trigger with a checkbox that will make the collider attached to it a trigger instead. Now you can use the functions
OnTriggerEnter(collider object){
}
and
OnTriggerExit(collider object)(
}
to write code for when an object enters the trigger sphere and when it leaves
basically whenever an object enters the sphere around the mine that is the collider you added marked as a trigger unity will automatically Call
OnTriggerEnter(collider objectThatEnteredCollider);
So your mine will get told an object entered and be given the collider of that object so it can do stuff with that object (access its transform, compare and find out what type of object it is etc)
so for example you can do a
OnTriggerEnter(collider EnemyCollider){
//check to make sure we found the player
if(EnemyCollider.gameobject.tag == "player"){
//if it is lets begin moving toward it
//trackplayer is just a bool we will use in update, if its true we will move the mine //towards the player, if it's false we wont, this is because ontriggerenter is only called //once, the moment the player enters, it isnt called over and over while he stays there.
//There is a function called OnTriggerStay but using update is just as effective.
TrackPlayer = true;
//later you can write the OnTriggerExit function, basically it will just be the line
//TrackPlayer = false, so when they get far enough away you stop tracking towards them
//TrackPlayer should start out false of course.
//now lets check the distance and see if were close enough to explode using the built in
//distance function unity supplies
if(vector3.Distance(mine.transform.position,EnemyCollider.gameobject.transform.position) < ExplosionDistance){
Destroy(Mine,5);
//damage player
}
//close the original if statement
}
//close the ontriggerenter statement
}