简体   繁体   中英

Unity2D: Move enemy towards object when spotted

I have a script that will make enemies look around an area for random objects within a scene, if the enemy spots the object then the enemy will move forward. However, this is not what I want the enemy to do, instead of the enemy moving forward, I want the enemy to move towards the object and then once the enemy reaches the object, I want the enemy to stop it's movement. I have different types of objects with different tags attached to it so I don't think moving the object using the object's tag name will be effective. I hope to find a way to move the enemies to the nearest object using the object's Layer name as all of my objects have the same layer name, meaning whatever object is spawn in to the scene the enemy will be able to move towards it. Is there a way to move my enemy towards an object when they have the object in sight? Thank you for your help!

This is my code so far:

public Transform sightStart, sightEnd, sightStart2, sightEnd2;

public Vector3 direction = Vector3.right;
public Vector3 direction2 = Vector3.right;

public float speed = 2f;

public bool spotted = false;
public bool rotate, moveEnemy = false;

public Camera mainCam;

public GameObject EndSight, EndSight2;

void Start () {
    speed = 2f;
    spotted = false;
    rotate = false;
    moveEnemy = false;
    mainCam = GameObject.Find ("Main Camera").GetComponent<Camera> ();

}


void Update () {
        Behaviours ();
    if (mainCam.WorldToScreenPoint (this.transform.position).x > Screen.width / 2) {
        direction.x = -1;
            InvokeRepeating ("EnemySight", 0, 10f); 
        if (moveEnemy == false) {
            RayCasting (); 
            Destroy (gameObject, 7f);
        } 
    } else {
        direction.x = 1;
            InvokeRepeating ("EnemySight", 0, 10f); 
        if (moveEnemy == false) {
            RayCasting2 (); 
            Destroy (gameObject, 7f);
        }
    }
}

void EnemySight() {
    if (rotate == false) {
        if (mainCam.WorldToScreenPoint (this.transform.position).x > Screen.width / 2) {
            EndSight.transform.Translate (direction2 * speed * Time.deltaTime);
            direction2.x = 0;
            direction2.y = 1;
            StartCoroutine (wait1 ());
        } else {
            EndSight2.transform.Translate (direction2 * speed * Time.deltaTime);
            direction2.x = 0;
            direction2.y = 1;
            StartCoroutine (wait1 ());

        }
    } else if (rotate == true) {
        if (mainCam.WorldToScreenPoint (this.transform.position).x > Screen.width / 2) {
            EndSight.transform.Translate (direction2 * speed * Time.deltaTime);
            direction2.x = 0;
            direction2.y = -1;
            StartCoroutine (wait2 ());
        } else {
            EndSight2.transform.Translate (direction2 * speed * Time.deltaTime);
            direction2.x = 0;
            direction2.y = -1;
            StartCoroutine (wait2 ());
        }
    }
}

IEnumerator wait1() {
    yield return new WaitForSeconds (0.7f);
    rotate = true;
}

IEnumerator wait2() {
    yield return new WaitForSeconds (0.7f);
    rotate = false;
}

void RayCasting()
{
    Debug.DrawLine (sightStart.position, sightEnd.position, Color.black);
    spotted = Physics2D.Linecast (sightStart.position, sightEnd.position, 1 << LayerMask.NameToLayer("Items"));

}

void RayCasting2()
{
    Debug.DrawLine (sightStart2.position, sightEnd2.position, Color.black);
    spotted = Physics2D.Linecast (sightStart2.position, sightEnd2.position, 1 << LayerMask.NameToLayer("Items"));

}

void Behaviours()
{
    if (spotted == true) {
        transform.Translate (direction * speed * Time.deltaTime);
        speed = 2f;
    } else if (spotted == false) {
        speed = 0;
    }
} 

void OnTriggerExit2D (Collider2D col) {
    if (col.tag == "object1") {
        speed = 0;
        CancelInvoke();
    }
    if (col.tag == "object2") {
        speed = 0;
        CancelInvoke();
    }
    if (col.tag == "object3") {
        speed = 0;
        CancelInvoke();
    }
    if (col.tag == "object4") {
        speed = 0;
        CancelInvoke();
    }
  }
}

https://docs.unity3d.com/ScriptReference/Vector2.MoveTowards.html

enemy.transform = Vector2.MoveTowards(enemy.transform, target.transform, speed * Time.deltaTime);

If you want to stop the enemy a bit farther than the transform of the target, your target could be the hitPoint of the Raycast.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM