Search Unity

How to see if an object is close to player?

Discussion in 'Scripting' started by ZarkonesGames, Nov 28, 2015.

  1. ZarkonesGames

    ZarkonesGames

    Joined:
    Feb 22, 2015
    Posts:
    5
    I am making RTS game (beginner project), and in game I have workers that you can select and move, but I want to implement monsters (like spider) and if that spider is near 500 pixels to worker, it will attack, but how to program that in C#?
    Sorry for my english.
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Investigate trigger colliders and OnTriggerEnter.
     
  3. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    This could help also:
    Code (CSharp):
    1. Vector3 PlayerPosition = Player.transform.position;
    2. Vector3 EnemyPosition = Enemy.transform.position;
    3. Vector3 PlayerDistanceToEnemy = Vector3.Distance(PlayerPosition,EnemyPosition);
    4. if (PlayerDistanceToEnemy < 5.0f) {
    5.     EnemyAttacks();
    6. }