- Home /
How can i execute a code when a trigger enter is called
When i run this code, my enemy just execute the Vector3.Movetowards, even though it does not yet collided.
you can see my declaration collided bool is default to false, and just set it to true when in TriggerEnter is called, i cannot get the output. which it will only run Vector3.Movetowards when it triggers. Help plz. Thanks in advance
using UnityEngine; using System.Collections;
public class Enemymovement : MonoBehaviour {
public static int scoreValue = 0;
public int Initialscore = 0;
public float velocity = 1f;
private GameObject destination;
private float speed = 10f;
private float step = 10;
private bool collided = false;
private FishInstantiate fishscript;
void Awake()
{
fishscript = GetComponent<FishInstantiate> ();
}
void Update()
{
if (collided = true) {
transform.position = Vector3.MoveTowards(transform.position, destination.transform.position, step);
}
}
void Start ()
{
destination = GameObject.Find("Destination");
scoreValue = Initialscore;
rigidbody2D.velocity = new Vector2 (-velocity, rigidbody2D.velocity.y);
Destroy(gameObject, 10);
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Destination") {
collided = true;
ScoreManager.score += scoreValue;
}
}
}
Comment