Search Unity

Question Help with raycasts

Discussion in 'Scripting' started by BetrayedPickle, Jun 30, 2020.

  1. BetrayedPickle

    BetrayedPickle

    Joined:
    Mar 30, 2019
    Posts:
    119
    I want to make a script so if a raycast hits a specific object, then it will check if 3 of my booleans equal true, and if it is true, then the player can press "E" to interact. How could I do this? If anyone knows how I could do that please let me know.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
    There are some Raycast examples here: https://docs.unity3d.com/ScriptReference/Physics.Raycast.html
    And here you can find a couple examples for how to respond to a keypress: https://docs.unity3d.com/ScriptReference/Input.GetKeyDown.html

    So you can get 5 booleans:
    • your 3 existing booleans
    • 1 boolean if the raycast is successful
    • 1 boolean for "the player pressed 'E' this frame"
    Take these 5 booleans and AND them together with the "&&" operator. If that result is true, perform your interaction. While writing this you should Debug.Log all of these values to make sure each one is working properly.