RaycastHit.normal not correct

Hi gays,

I have problems with get “normal” vector from RaycastHit.

I created simple scheme, appended plane and raycast to plane. I got collision and raycastHit result.
In results i have collision point (it’s good) and normal vector (It’s good only if collision point (plane.position) have x and z coordinates equal zero (0). Even if rotate plane - Normal vector is correct. If i change x or z coordinate of plane - normal vector is wrong.
Who know why?

My code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RaycastTest : MonoBehaviour
{

    public GameObject plane;
    RaycastHit hit;
    Ray ray;


    // Start is called before the first frame update
    void Start()
    {

        Vector3 dir = plane.transform.position - transform.position;
        dir = Vector3.Normalize(dir); // Without normalizing same result
        ray = new Ray(transform.position, dir);
        Physics.Raycast(ray, out hit);

        if (hit.collider != null)
        {
            Debug.Log("Hit");
        }

    }

    // Update is called once per frame
    void Update()
    {
        Debug.DrawLine(ray.origin, hit.point, Color.red);
        Debug.DrawLine(hit.normal, hit.point, Color.green);
    }
}

P.s. In screen mistake - it looks like normals always want be (0,1,0)


Problem founded and i will keep post if someody will do same error:

Normal it’s vector from Vector3.zero (0,0,0) to normal Vector. So i should look normal in next way:
Debug.DrawLine(hit.normal + hit.point, hit.point, Color.green);

4 Likes