Question by
LeGrandeHeidi · Mar 10, 2017 at 07:05 AM ·
unity 5raycastvector3hit
RaycastHit.normal and Vector3.reflect problem
So i'm having an issue with a simple project of mine. I want to reflect a ray (Linerenderer) with a "mirror". when I'm using hit.normal the "light" is not reflected correctly. It's is really driving me crazy at the moment.
heres a simple screenshot and my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Beam : MonoBehaviour
{
private LineRenderer lr;
void Start()
{
lr = GetComponent<LineRenderer>();
}
void Update()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, Vector3.forward, out hit, Mathf.Infinity))
{
lr.SetPosition(0, transform.position);
lr.SetPosition(1, hit.point);
if (hit.collider)
{
Vector3 pos = Vector3.Reflect(hit.point - this.transform.position, hit.normal);
lr.SetPosition(2, pos);
}
}
else
{
lr.SetPosition(2, new Vector3(0, 0, 2000));
}
}
}
mirrorissue.png
(211.7 kB)
Comment