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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Issue with Update and Raycast

Discussion in 'Scripting' started by PhysicalBeef, Dec 9, 2015.

  1. PhysicalBeef

    PhysicalBeef

    Joined:
    Jun 13, 2013
    Posts:
    150
    So, I want to call these functions once:
    Code (csharp):
    1.  
    2. function CrossHairEnable () {
    3.     cr2.gameObject.SetActive(true);
    4.     cr1.gameObject.SetActive(false);
    5.     cr2.transform.localScale = Vector3.Lerp(closed, open, 50*Time.deltaTime);
    6. }
    7.  
    8. function CrossHairDisable () {
    9.     cr2.transform.localScale = Vector3.Lerp(open, closed, 50*Time.deltaTime);
    10.     cr2.gameObject.SetActive(false);
    11.     cr1.gameObject.SetActive(true);
    12. }
    13.  
    And I need to do it through this:
    Code (csharp):
    1.  
    2. function Update () {
    3.     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    4.     var hit : RaycastHit;
    5.     crNormal = true;
    6.     if (Physics.Raycast (ray, hit, 3)) {
    7.         if(hit.transform.tag == "npc") {
    8.             crNormal = false;
    9.         }
    10.         if(hit.transform.tag == "door") {
    11.             crNormal = false;
    12.         }
    13.     }
    14.  
    15.     if(crNormal) {
    16.         CrossHairDisable();
    17.     }else{
    18.         CrossHairEnable();
    19.     }
    20. }
    21.  
    How could I go around this? If I use the "off -> on -> off when you need it again" thing, the ray won't re-cast and thus I won't be able to exit it. With how I've made this, it seems pretty hard. (At least to me.)
     
  2. RavenMikal

    RavenMikal

    Joined:
    Oct 18, 2014
    Posts:
    144
    have you tried adding another boolean?

    ie:
    Code (csharp):
    1.  
    2. if(crNormal){
    3.        if(!chd){
    4.             CrossHairDiable();
    5.             chd = true;
    6.             che = false;
    7.         }
    8. }else{
    9.         if(!che){
    10.               CrossHairEnable();
    11.               che = true;
    12.               chd = false;
    13.          }
    14. }
    15.  
     
  3. PhysicalBeef

    PhysicalBeef

    Joined:
    Jun 13, 2013
    Posts:
    150
    What would che and chd be?
     
  4. PhysicalBeef

    PhysicalBeef

    Joined:
    Jun 13, 2013
    Posts:
    150
    Also, since the look-at rayacst thing is being called all the time, I need it to activate, and then shut off, until you look at something else, say air or object.
     
  5. RavenMikal

    RavenMikal

    Joined:
    Oct 18, 2014
    Posts:
    144
    Che and chd would just be two thrown together booleans, short for crosshairenable and crosshairdisable. And your right, I misunderstood that it was the raycaster you were trying to turn off.

    problem with what your asking is, how would it know what it was looking at without the raycaster? Turning it off will be easy, but you won't be able to turn it on based on what they see, because without a raycaster it doesn't.

    I guess you could turn it back on as soon as they're camera changes rotation, but I'm not sure thats what your looking for there. So for that you would keep up with what they were looking at by position, but that seems alot more complicated then using a raycast >_<

    but if you did want to try that, just have a main camera rotation check in update, whenever its different from the last check, you know its move, you can re enable then disable after a time using a timestamp.

    and honestly I figured someone with more experience would be trying to answer this by now...lol, so for that I apologize.
     
  6. flonch

    flonch

    Joined:
    Aug 20, 2014
    Posts:
    63
    I haven't tested this, but I think you're looking for something like this. It's basically the same thing RavenMikal suggested. Sorry if there's some dumb error

    Code (JavaScript):
    1.  
    2.   var mousePos:Vector3=Vector3.zero;
    3.    var crNormal:boolean=true;
    4.  
    5.    function CrossHairToggle() {
    6.      cr1.gameObject.SetActive(crNormal);
    7.      cr2.gameObject.SetActive(!curNormal);
    8.      if (crNormal)
    9.        cr2.transform.localScale=Vector3.Lerp(open, closed, 50*Time.deltaTime);
    10.      else
    11.        cr2.transform.localScale=Vector3.Lerp(closed, open, 50*Time.deltaTime);
    12.      crNormal=!crNormal;
    13.    }
    14.  
    15.    function Update () {
    16.      if (mousePos!=Input.mousePosition) {
    17.        mousePos=Input.mousePosition;
    18.  
    19.        var ray = Camera.main.ScreenPointToRay (mousePos);
    20.        var hit : RaycastHit;
    21.        if (Physics.Raycast (ray, hit, 3)) {
    22.          var tag:String=hit.transform.tag;  
    23.          if (tag=="npc" || tag=="door") {
    24.            if (crNormal)
    25.              CrossHairToggle();
    26.          }
    27.          else if (!crNormal)
    28.            CrossHairToggle();
    29.        }
    30.      }
    31.    }
    CrossHairToggle() toggles the value of crNormal, so when the function is executed, it won't execute again until the raycast hits the opposite type of object.
    Let's say npc/door represents typeA, and everything else represents typeB. If your raycast hits a typeA object, CrossHairToggle() is executed and crNormal is toggled. If you hit another typeA object, without hitting a typeB object in between the two typeA hits, CrossHairToggle() will not be executed.

    Edit:
    If you want to be more efficient with the raycasts, you could just check if the mouse position has changed, and raycast only if there has been a change. It's not 100% what you're looking for, since it will still raycast if you're looking at the same object while moving the mouse slightly. I'm not sure how you could disable the raycasts completely after hitting one object, and enable it once you hit something else.
     
    Last edited: Dec 11, 2015