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

Raycast Distance not working correctly

Discussion in 'Physics' started by Badseed619, Jun 18, 2022.

  1. Badseed619

    Badseed619

    Joined:
    Oct 8, 2021
    Posts:
    2
    I'm not sure if this is a bug or if I'm doing something wrong. I have a simple Raycast script that I simply want to detect an object with. I have set the maxDistance variable to 8f and it still picks up items further than that. I know this because my script says to leave the screen inactive until the raycast distance is less than 5.5.. But, even items farther than that will trigger the text screen. When I'm close to an object, it works fine. The text screen won't appear, when I move <=5.5f it appears. From that same position, I can aim to another object and the text screen appears again, when it shouldn't because it's too far away.. Anyone have any thoughts?

    // Detect object Code... I also made this a regular function and just put that in update. Even worse.
    // gameController is my script manager. All scripts are connected here.

    public void OnMouseEnter(){
    if (gameController.playerCast.hit.distance <= 5.5f){
    gameController.itemDescription.text = "Rock";
    gameController.buttonText.text = "Space to Collect";
    gameController.itemDetailsParent.SetActive(true);
    }
    }

    public void OnMouseExit(){
    gameController.itemDetailsParent.SetActive(false); }


    // Raycast code:

    void Update(){
    LayerMask layerToMask = LayerMask.GetMask("Investigate");

    // Raycast from player to objects with 'investigate' tag
    if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, 8f, layerToMask)){
    toTarget = hit.distance;
    hitName = hit.transform.name;
    hitObject = hit.collider.gameObject;
    }
     
  2. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    777
    Hi,

    Perhaps the colliders is not exactly in the positions relative the gameObjects, as you think. Or the rays is not going in the direction, as you think, also resulting in hitting other objects than you expect to be hitted.

    Some suggestions for figuring out what's going on might be to simplify the scene to just one, or a few objects. And using Debug.DrawRay to visualize the rays when running the game in the editor.
     
    Last edited: Jun 18, 2022
  3. Badseed619

    Badseed619

    Joined:
    Oct 8, 2021
    Posts:
    2
    @arfish Thanks for helping!
    The ray is correct as I do draw it and have a Debug message so I can make sure it's getting hit correctly.

    All I want to do is simply hover over an item, display the item info and button text to collect (destroy) it..
    If you don't destroy it, and look away, the text disappears. I must be going about this all wrong.

    If I use OnMouseEnter, it picks up every item in the scene, regardless of distance. I can use an IF statement with ray.distance as a nested IF and it still picks up every object in view, regardless of distance.

    I try to use the raycast and when I exit the object, the text remains as I can't find an IF statement to remove the hitObject data...... if(ray.distance < 5f) { display..message} <--- How do I disable the message after this?

    I did try if(ray.distance > 5f), and that still didn't work....
     
  4. dsv202

    dsv202

    Joined:
    Jun 6, 2013
    Posts:
    1

    Might be too late to answer...but for other's sake, I had a similar issue - I set my detect distance to let's say 5f, get close enough to whatever object I'm displaying info for and hover over it. The last object hovered over will stick as the latest thing hit by the ray until the rays pickup something else. To get around this, you need the ray to pickup anything else once you are no longer targeting the object. Once you display the info, set your distance to like 1000f, so immediately when you move the targeting off the object it hits anything in your scene. Then once the info pane drops, you can put your distance back to 5f.