Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Let a enemy attack when he sees the player

Discussion in 'Getting Started' started by TheSaviour01, Dec 17, 2019.

  1. TheSaviour01

    TheSaviour01

    Joined:
    Apr 2, 2016
    Posts:
    30
    Hey there,
    Can someone help me

    im using this code to try to let de enemy see
    so he look if he can see the player or not.

    But the problem is he can still see me whem im behind a wall
    Can someone help me out ?

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.AI;
    6. using UnityEngine.SceneManagement;
    7.  
    8. public class Test : MonoBehaviour
    9. {
    10.     [SerializeField]
    11.     float distance;
    12.  
    13.     public GameObject player;
    14.     public Transform Shootpoint;
    15.  
    16.     public bool CanSee = false;
    17.  
    18.     [SerializeField]
    19.     float chaseDistence = 0.5f;
    20.  
    21.  
    22.  
    23.     void Update()
    24.     {
    25.         distance = Vector3.Distance(Shootpoint.position, player.transform.position);
    26.  
    27.         if (!Physics.Raycast(Shootpoint.position, player.transform.position, chaseDistence))
    28.         {
    29.             Debug.DrawLine(Shootpoint.position, player.transform.position, Color.red);
    30.             CanSee = false;
    31.             Debug.Log("CANT SEE");
    32.  
    33.         }
    34.         else if (Physics.Raycast(Shootpoint.position, player.transform.position, chaseDistence))
    35.         {
    36.             Debug.DrawLine(Shootpoint.position, player.transform.position, Color.green);
    37.             CanSee = true;
    38.             Debug.Log("CAN SEE");
    39.         }
    40.     }
    41. }
    42.  
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,851
    You're smart to use Debug.DrawLine to see what's going on. So the ray is going right through the wall? That should only be possible if the wall does not have a collider (or its collider is set to Trigger, or something like that).
     
  3. TheSaviour01

    TheSaviour01

    Joined:
    Apr 2, 2016
    Posts:
    30
    i have do have a box collider around the wall yes but still it's going right through
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Physics.Raycast returns true if it hits any collider. You need to check what collider it hits to verify if it is the player or an obstacle. It is probably hitting the wall, returns true, and you are assuming true means it is hitting the player. Also, use just "else" instead of "else if" because the way you have it written you are doing the same raycast twice.

    Relevant thread:
    https://forum.unity.com/threads/raycast-going-through-other-objects.385632/

    Scripting reference:
    https://docs.unity3d.com/ScriptReference/Physics.Raycast.html
     
    Hatfield and JoeStrout like this.
  5. abdelrahmanyoussry509

    abdelrahmanyoussry509

    Joined:
    Jan 26, 2020
    Posts:
    14
    i want to do something similar to yours but i dont understand something
    the raycast only is a line so how does he see you if you are not touching the raycast?
    you could be in a diffrent angle than the raycast
    (sorry i am new so if you can explain it to me i would really apreciate it)