Calculate normal vector to the plane that was hit by raycast

70983-redvector.png

How do I go about calculating the red vector in 3D space? I’m using Raycast to hit a gameobject, and I need to create another object with rotation equal to the direction the red vector is pointing.

Info I have available to complete this task is: raycast hit info, and perhaps more, but I’m a noob, and I don’t have any skill nor experience in unity, nor am I a good mathematician (I suck at maths)

1 Answer

1

Use the RaycastHit.normal property. Here’s an example;

using UnityEngine;
using System.Collections;

public class RayCastWithNormal : MonoBehaviour {
	
	void Update () {
        RaycastHit raydata;
        if (Physics.Raycast(transform.position, transform.forward, out raydata))
        {
            Debug.DrawLine(transform.position, raydata.point, Color.red);
            Debug.DrawRay(raydata.point, raydata.normal*1000, Color.red);
        }
        else
        {
            Debug.DrawRay(transform.position, transform.forward*1000, Color.black);
        }
	}
}