Question by
VeryBigNoob · May 28, 2016 at 10:03 PM ·
raycast3dvector3
Calculate normal vector to the plane that was hit by raycast
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)
redvector.png
(5.6 kB)
Comment
Best Answer
Answer by Maurdekye · May 29, 2016 at 01:56 AM
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);
}
}
}