Search Unity

Resolved Want a way to touch on object to look at it then touch anywhere else to stop looking

Discussion in 'Scripting' started by Lazy_Fish_Studios, May 21, 2020.

  1. Lazy_Fish_Studios

    Lazy_Fish_Studios

    Joined:
    Jan 9, 2017
    Posts:
    8
    When I touch on a gameobject called "enemy" or "enemy1" my player looks at them but when I touch on the UI joystick it stops looking at them. I need a way to look at the enemy until I tap anywhere else on the screen (excluding UI joystick) but also if I touch "enemy1" change the LookAt to that

    Thanks

    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (movementEnabled == true)
    4.         {
    5.             Move();
    6.         }
    7.  
    8.         if (lookAtEnemyEnabled == true)
    9.         {
    10.             LookAtTarget();
    11.         }
    12.  
    13.         if (Input.GetMouseButtonDown(0))
    14.         {
    15.             Ray ray = cam.ScreenPointToRay(Input.mousePosition);
    16.             RaycastHit hit;
    17.  
    18.             //Hit specific layers
    19.             if (Physics.Raycast(ray, out hit, 300, hittable))
    20.             {
    21.                 Debug.Log(hit.transform.name);
    22.             }
    23.  
    24.             //Hit specific game objects
    25.             if (Physics.Raycast(ray, out hit, 300))
    26.             {
    27.                 if (hit.collider.gameObject.name == "enemy")
    28.                 {
    29.                     lookAtEnemyEnabled = true;
    30.                     lookAtEnemyIndex = 0;
    31.                 }
    32.                 else if (hit.collider.gameObject.name == "enemy1")
    33.                 {
    34.                     lookAtEnemyEnabled = true;
    35.                     lookAtEnemyIndex = 1;
    36.                 }
    37.             }
    38.         }
    39.  
    40.         if (lookAtEnemyIndex == 0)
    41.         {
    42.             playerMustLookAt = GameObject.Find("enemy").transform;
    43.         }
    44.  
    45.         if (lookAtEnemyIndex == 1)
    46.         {
    47.             playerMustLookAt = GameObject.Find("enemy1").transform;
    48.         }
    49.     }
     
  2. Lazy_Fish_Studios

    Lazy_Fish_Studios

    Joined:
    Jan 9, 2017
    Posts:
    8
    Found a solution shortly after posting

    I created a new private int called NOThittable, this selects all layers except the ones the enemies are on

    then added an "else if" section into the raycast and it all seems to work fine :)

    Updated code below

    Code (CSharp):
    1. if (Input.GetMouseButtonDown(0))
    2.         {
    3.             Ray ray = cam.ScreenPointToRay(Input.mousePosition);
    4.             RaycastHit hit;
    5.  
    6.             //Hit specific game objects
    7.             if (Physics.Raycast(ray, out hit, 300, hittable))
    8.             {
    9.                 if (hit.collider.gameObject.name == "enemy")
    10.                 {
    11.                     lookAtEnemyEnabled = true;
    12.                     lookAtEnemyIndex = 0;
    13.                 }
    14.  
    15.                 if (hit.collider.gameObject.name == "enemy1")
    16.                 {
    17.                     lookAtEnemyEnabled = true;
    18.                     lookAtEnemyIndex = 1;
    19.                 }
    20.             }
    21.  
    22.             else if (Physics.Raycast(ray, out hit, 300, NOThittable))
    23.             {
    24.                 lookAtEnemyEnabled = false;
    25.                 lookAtEnemyIndex = -1;
    26.             }
    27.         }