Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Why Raycast(Ray,Hit,Distance) is not working?

Discussion in 'Scripting' started by wechat_os_Qy0yVLu9ot1btp3LlgxMjCt0k, May 28, 2020.

?

Why Raycast(Ray,Hit,Distance) is not working?

  1. 1

    10.5%
  2. 2

    21.1%
  3. 3

    21.1%
  4. 4

    21.1%
  5. 5

    26.3%
  1. wechat_os_Qy0yVLu9ot1btp3LlgxMjCt0k

    wechat_os_Qy0yVLu9ot1btp3LlgxMjCt0k

    Joined:
    May 28, 2020
    Posts:
    4
    I have some code like this (pesudo code):

    ray.direction=transform.forward;

    if (Physics.Raycast(ray, out hit,5f) )
    { Debug.Log( " hit someting ") ; }
    else{
    Debug.Log("hit nothing " + ray.direction);
    }
    Debug.DrawRay(transform.position,ray.direction*5f, Color.red);
    }

    and the Physics.Raycast method always returing "false"....
    I don't now why...
    1. the distance from ray source to collider is << than 5f;
    2. the object the ray hits has a Box Collider,which is not trigger
    3. the ray source is not in any Collider..
    upload_2020-5-28_21-14-45.png
    the red line point to right is the Debug.DrawRay() result...
    from this pic, I think the direction is correct..

    sorry for my poor English...
     
  2. wechat_os_Qy0yVLu9ot1btp3LlgxMjCt0k

    wechat_os_Qy0yVLu9ot1btp3LlgxMjCt0k

    Joined:
    May 28, 2020
    Posts:
    4
    sorry for the usless Polll,, I don't how to post a question whiout a poll...
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,893
    Can you share your actual code? The pseudocode you posted is missing a lot.
     
  4. wechat_os_Qy0yVLu9ot1btp3LlgxMjCt0k

    wechat_os_Qy0yVLu9ot1btp3LlgxMjCt0k

    Joined:
    May 28, 2020
    Posts:
    4

    ---

    thanks for replying ,,this is the whole code:


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class Check4Wall : MonoBehaviour {
    public Vector3
    front_wall_noraml, back_wall_normal,
    left_wall_normal, right_wall_normal;
    public float distance_to_front, distance_to_back,
    distance_to_right, distance_to_left;
    Ray front_ray, back_ray, right_ray,left_ray;
    RaycastHit front_hit, back_hit, right_hit, left_hit;
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
    front_ray.direction = transform.forward;
    back_ray.direction = transform.forward * -1f;
    right_ray.direction = transform.right;
    left_ray.direction = transform.right * -1f;
    checkWall(front_ray,out front_hit,out front_wall_noraml,out distance_to_front,Color.red);
    // checkWall(back_ray, out back_hit, out back_wall_normal, out distance_to_back,Color.white);
    // checkWall(right_ray,out right_hit,out right_wall_normal,out distance_to_right,Color.green);
    //checkWall(left_ray, out left_hit, out left_wall_normal, out distance_to_left,Color.yellow);

    }
    void checkWall(Ray ray,out RaycastHit hit,out Vector3 wall_normal,out float distance,Color color){

    if (Physics.Raycast(ray, out hit,5f)
    //&& hit.transform.tag.Equals("wall")
    )
    { wall_normal = hit.normal;
    distance = hit.distance;
    }
    else{
    wall_normal = new Vector3(0f, 0f, 0f);
    distance = 1f;
    Debug.Log("hit nothing " + ray.direction);
    }
    Debug.DrawRay(transform.position, ray.direction*5f,color);
    Debug.DrawRay(hit.point, hit.normal);

    }
    }
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,893
    Rays consist of two parts:
    • An origin point (Vector3)
    • A direction (also Vector3)
    Your code is setting the direction:
    Code (CSharp):
    1. front_ray.direction = transform.forward;
    But it's not setting the origin. So Unity assumes that the origin of your ray is (0,0). It's not moving around with your player. Try setting the origin of the rays as well as the direction:
    Code (CSharp):
    1. front_ray.direction = transform.forward;
    2. front_ray.origin = transform.position;
     
  6. wechat_os_Qy0yVLu9ot1btp3LlgxMjCt0k

    wechat_os_Qy0yVLu9ot1btp3LlgxMjCt0k

    Joined:
    May 28, 2020
    Posts:
    4