Search Unity

Text doesn't show unless looking at very specific part of door.

Discussion in 'Scripting' started by Dills60, Jun 20, 2021.

  1. Dills60

    Dills60

    Joined:
    Aug 12, 2018
    Posts:
    7
    I have made a script that casts a ray from the camera, and detects if there is a door in front. It should then display text on the screen that says "Press F to open". This used to work fine, but for some reason that I cant find, it now only displays the text when the player looks directly at a very specific part of the door. I added a Debug.Log to make sure it was the text that wasn't working and not the raycast. The log shows fine, but just not the text.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class RayCast : MonoBehaviour
    7. {
    8.     public Text pressToOpen;
    9.     public bool fPressed = false;
    10.     public bool isLooking = false;
    11.     public bool leftDoor;
    12.     public GameObject player;
    13.     private void Start()
    14.     {
    15.         pressToOpen.text = "";
    16.  
    17.        
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void FixedUpdate()
    22.     {
    23.         int layerMask = 1 << 8;
    24.  
    25.         Vector3 fwd = transform.TransformDirection(Vector3.forward);
    26.  
    27.         if (Physics.Raycast(transform.position, fwd, 10, layerMask))
    28.         {
    29.            
    30.  
    31.             Debug.Log("Looking at door");
    32.  
    33.             isLooking = true;
    34.  
    35.            
    36.         }
    37.  
    38.         if (Physics.Raycast(transform.position, fwd, 10, ~layerMask))
    39.         {
    40.             isLooking = false;
    41.         }
    42.      
    43.        
    44.     }
    45.  
    46.     private void Update()
    47.     {
    48.         if (isLooking == true)
    49.         {
    50.             pressToOpen.text = "Press F to open";
    51.             if (Input.GetKeyDown(KeyCode.F))
    52.             {
    53.                 Debug.Log("Opened");
    54.                 fPressed = true;
    55.             }
    56.             else
    57.             {
    58.                 fPressed = false;
    59.             }
    60.         }
    61.  
    62.         if (isLooking == false)
    63.         {
    64.             pressToOpen.text = "";
    65.         }
    66.  
    67.      
    68.     }
    69.  
    70.    
    71. }
    72.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Instead of lines 38-41, why not just put
    isLooking = false;
    on line 26?

    That way if you don't hit it, it will be off.

    Also, in Update(), Debug.Log() the isLooking variable.

    ALSO: if you have more than one of these things, you will need a different text object for each one, otherwise they'll fight for control of the one text object.
     
  3. Dills60

    Dills60

    Joined:
    Aug 12, 2018
    Posts:
    7

    I only have the one door at the moment, and as I said, the script was working fine. I feel I might have changed something by accident and can't see it by myself

    EDIT: Nevermind, it has just started working again
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    BTW, line 25 can be shortcutted as:

    Vector3 fwd = transform.forward;


    And to simplify things, no reason you can't do the raycast inside of Update(), right before you decide. That lets you get away from class-level variables like isLooking...

    ALSO: line 58 above clearing fPressed is kinda weird because if you were pressed when looking, then looked away, fpressed would stay true until you looked at a door again. The usual pattern is to clear it every frame, then set it when it trips, or call whatever you want to happen.