Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

raycast nearest enemy

Discussion in 'Scripting' started by kantaki, Mar 30, 2012.

  1. kantaki

    kantaki

    Joined:
    May 15, 2011
    Posts:
    254
    Code (csharp):
    1. void attackEnemyNear ()
    2.     {
    3.         GameObject[] nearestEnemy = GameObject.FindGameObjectsWithTag ("Enemy");
    4.    
    5.         if (nearestEnemy.Length == 0) {
    6.             return;
    7.         } else if (nearestEnemy.Length == 1) {
    8.            
    9.             RaycastHit hit;
    10.            
    11.             if (Physics.Raycast (transform.position,nearestEnemy[0].transform.position - transform.position , out hit)) {
    12.                    
    13.                 if (hit.distance <= sightRange) {
    14.                     target = nearestEnemy [0].transform;
    15.                 }
    16.             }
    17.        
    18.    
    19.         }
    20.    
    21.     }
     
    Last edited: Mar 30, 2012
  2. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    what kind of game is this for? I'm thinking the player automatically targets enemys once they get in range and sees the enemy? It might help to see the whole script since theirs names in the example that have not been declared variables.
     
    Last edited: Mar 30, 2012
  3. kantaki

    kantaki

    Joined:
    May 15, 2011
    Posts:
    254
    its for an rts / diablo like game, i think i found a solution. i will update my code in a few seconds

    updated: but its only for 1 enemy atm
     
  4. Tobias J.

    Tobias J.

    Joined:
    Feb 21, 2012
    Posts:
    423
    I believe your problem is that you're using two positions in the raycast, where you'd want a position and a direction.

    Try:

    Code (csharp):
    1.  
    2. void attackEnemyNear ()
    3.     {
    4.         GameObject[] nearestEnemy = GameObject.FindGameObjectsWithTag ("Enemy");
    5.         if (nearestEnemy.Length == 0) {
    6.             return;
    7.         } else if (nearestEnemy.Length == 1) {
    8.             RaycastHit hit;
    9.             Vector3 direction = transform.position - nearestEnemy[0].transform.position;
    10.             if (Physics.Raycast (transform.position, direction, out hit)) {
    11.             Debug.Log(hit.distance);
    12.                 if (hit.distance <= sightRange) {
    13.                     target = nearestEnemy [0].transform;
    14.                 }
    15.             }
    16.         }  
    17.     }
    18.  
    hm. or perhaps it's Vector3 direction = nearestEnemy[0].transform.position - transform.position;

    Hope it helps.
     
  5. kantaki

    kantaki

    Joined:
    May 15, 2011
    Posts:
    254
    you are 1 min too late :) , already found a soultion by playing with the Debug.Draw cmd.

    but thanks anyways.



    Solved