Search Unity

Entity not detected by Event System nor Raycast

Discussion in 'General Discussion' started by Donelfefe, Mar 29, 2019.

  1. Donelfefe

    Donelfefe

    Joined:
    Mar 24, 2019
    Posts:
    15
    Hello budddyyy,

    Has it's explained on the title, i can't detect an humanoid object on mouse hover.

    This entity is instantiated as well as my others entities. But the others don't have that problem.

    A little example clip, the entity is the moving player:



    some code:

    MyEntityCode
    Code (CSharp):
    1.  
    2.     private Ray ray;
    3.     private GetObject god;
    4.     private NavMeshAgent agent;
    5.     private Animator anim;
    6.  
    7.     [HideInInspector]
    8.     public bool isSelect;
    9.  
    10.     // Start is called before the first frame update
    11.     void Awake()
    12.     {
    13.         god = GameObject.Find("_god").GetComponent<GetObject>();
    14.  
    15.         if (!GetComponent<isClicked>())
    16.             gameObject.AddComponent<isClicked>();
    17.         gameObject.layer = 9;
    18.  
    19.         transform.position = new Vector3(god.buildingsRegister[god.buildingsRegister.Count - 1].transform.position.x +1, 0.2f, god.buildingsRegister[god.buildingsRegister.Count - 1].transform.position.z - 4);
    20.  
    21.         anim = GetComponent<Animator>();
    22.         isSelect = false;
    23.     }
    24.  
    25.     void Start()
    26.     {
    27.         gameObject.AddComponent<NavMeshAgent>();
    28.  
    29.         agent = GetComponent<NavMeshAgent>();
    30.  
    31.         agent.stoppingDistance = 2.5f;
    32.     }
    33.  
    34.     void FixedUpdate()
    35.     {
    36.         if (Input.GetButtonDown("Fire1"))
    37.         {
    38.             ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    39.             RaycastHit hitinfo;
    40.             if (Physics.Raycast(ray, out hitinfo))
    41.             {
    42.                 agent.SetDestination(hitinfo.point);
    43.             }
    44.         }
    45.  
    46.         if (agent.velocity.magnitude > 0.2)
    47.         {
    48.             if (agent.velocity.magnitude < 1.5)
    49.             {
    50.                 anim.SetBool("isWaiting", false);
    51.                 anim.SetBool("isRunning", false);
    52.                 anim.SetBool("isWalking", true);
    53.             }
    54.             else
    55.             {
    56.                 anim.SetBool("isWalking", false);
    57.                 anim.SetBool("isRunning", true);
    58.             }
    59.         }
    60.         else
    61.         {
    62.             anim.SetBool("isWalking", false);
    63.             anim.SetBool("isRunning", false);
    64.             anim.SetBool("isWaiting", true);
    65.         }
    66.     }
    67.  
     
    Last edited: Mar 29, 2019