Search Unity

Resolved How to set the length of a Raycast from screen

Discussion in 'Scripting' started by Victoralm, Aug 23, 2020.

  1. Victoralm

    Victoralm

    Joined:
    Dec 31, 2015
    Posts:
    30
    I'm trying to set the length of a Raycast but, till now without success... The ray keeps hitting an object that is way far from the 2.0f that I've set for its length... I'm using the Raycast to trigger the OnMouseOver () and OnMouseExit () of the interactable objects.

    I did try
    Code (CSharp):
    1. void Update () {
    2.         Vector3 startpos = this._cam.ScreenToWorldPoint (Input.mousePosition);
    3.         if (Physics.Raycast (startpos, Vector3.forward, out this._hit, 2.0f)) {
    4.             if (this._hit.transform.CompareTag ("Interactable")) {
    5.                 this._objectOriginalMaterial = _hit.transform.GetComponent<Renderer> ().material;
    6.             }
    7.             Debug.Log (this._hit.transform.name);
    8.         }
    9.     }
    and
    Code (CSharp):
    1. void Update () {
    2.         Ray ray = this._cam.ScreenPointToRay (Input.mousePosition);
    3.         if (Physics.Raycast (ray, out this._hit, 2.0f)) {
    4.             if (this._hit.transform.CompareTag ("Interactable")) {
    5.                 this._objectOriginalMaterial = _hit.transform.GetComponent<Renderer> ().material;
    6.             }
    7.             Debug.Log (this._hit.transform.name);
    8.         }
    9.     }
     
    Last edited: Aug 23, 2020
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    Your problem is vague, what is happening that it doesn't work for you?
     
  3. Victoralm

    Victoralm

    Joined:
    Dec 31, 2015
    Posts:
    30
    You're right @Chris-Trueman. My problem is that the ray keeps hitting an object that is way far from the 2.0f that I've set for its length...
     
  4. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    Try using Debug.DrawRay to see if its doing the 2.0f distance. A little background of what you are trying to do might help a bit, don't need to give me full details but an idea of what you are trying to achieve might help.
     
  5. Victoralm

    Victoralm

    Joined:
    Dec 31, 2015
    Posts:
    30
    I've just end up with it to get the behavior that I was expecting...
    Code (CSharp):
    1. void Update () {
    2.         Ray ray = this._cam.ScreenPointToRay (Input.mousePosition);
    3.  
    4.         if (Physics.Raycast (ray, out this._hit) && Vector3.Distance (this.transform.position, this._hit.transform.position) <= 2.5f) {
    5.             if (this._hit.transform.CompareTag ("Interactable")) {
    6.                 //
    7.                 this._hit.transform.GetComponent<IInteractable> ().IsObjInEvidence = true; // Changing the super class properties
    8.                 this._objectOriginalMaterial = _hit.transform.GetComponent<Renderer> ().material;
    9.             }
    10.             Debug.Log (this._hit.transform.name);
    11.         } else if (Physics.Raycast (ray, out this._hit) && Vector3.Distance (this.transform.position, this._hit.transform.position) > 2.5f) {
    12.             this._hit.transform.GetComponent<IInteractable> ().IsObjInEvidence = false; // Changing the super class properties
    13.         }
    14.     }
    Still think that this is a bit workaround ish...

    I'm using the Raycast to trigger the OnMouseOver () and OnMouseExit () of the interactable objects.
     
    Last edited: Aug 23, 2020
  6. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,555
    Vector3.Distance(ray.origin, this._hit.point)


    Edit: should be same as
    this._hit.distance
     
    Victoralm likes this.
  7. Victoralm

    Victoralm

    Joined:
    Dec 31, 2015
    Posts:
    30
    Thanks @polemical, it works too.
     
    adamgolden likes this.
  8. forteller

    forteller

    Joined:
    Jun 15, 2019
    Posts:
    55
    I assume what's happening here (although I could be wrong). Is that the "maxDistance" parameter in Physics.Raycast is intended to increase performance and not be used to center game logic around.

    I imagine that the maxDistance determines what spatial partitions in Unity's Physics world that have that have to be checked.
    If this is true, then maxDistance doesn't determine with precise floating point granularity what distance of GameObjects to return, but instead what spatial partitions (which each contain one or more GameObjects, and cover a large area) must be tested for raycast collisions.

    Although of course I don't know the actual implementation of Physics.Raycast so I can't be sure, but unless there's a bug in your code or Unity's, I imagine that's what's happening. If Unity were to only return the gameobjects with distances exactly under maxDistance, then that would represent a performance overhead for a pretty fundamental method which probably wouldn't be desirable. If this is the case, I think they should change their documentation so it's clear that the maxDistance parameter should be used purely for performance concerns (and not for game logic ).
     
    Last edited: Aug 24, 2020
  9. Victoralm

    Victoralm

    Joined:
    Dec 31, 2015
    Posts:
    30
    Hi @forteller, thanks for your reply!

    But I disagree with it. You should indeed use any resources at your disposal to improve the game logic/mechanics, IMO. To take what already exists in the engine to bring something that is on our plannings, appears to be exactly what we all are looking for while making games. But this, referring to my very comment, is just an opinion not an use case...
     
  10. forteller

    forteller

    Joined:
    Jun 15, 2019
    Posts:
    55
    Wasn't intending to make a comment on whether or not using maxDistance in this way is good or bad for a game engine. Just suggesting why it might work this way.

    But yeah, I basically agree with you -- if maxDistance works the way I suggested above, then it shouldn't be called maxDistance and should be called something else and made very clear that you can't use it for game logic purposes.
     
    Victoralm likes this.