How to reflect raycast rays of objects
I'm trying to write a script that draws a ray from a sphere in a direction of a specific wall and when that rays hits another wall it reflects of the wall, an if, in the new direction, it hits another wall, it reflects of that too.
This is what I have so far, all it does is draw the ray and draws a new ray if it hits a wall, and it only does it once but I want it to be continuous and the same ray.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AI : MonoBehaviour {
public GameObject wall;
private Vector3 dir;
private Vector3 origin;
private Vector3 fwd;
// Use this for initialization
void Start () {
dir= wall.transform.position - transform.position;
origin = transform.position;
}
// Update is called once per frame
void Update () {
//laser
DrawNewRay (origin, dir);
RaycastHit hit;
if(Physics.Raycast(origin, dir, out hit))
{
if(hit.collider.gameObject.CompareTag("Wall"))
{
Vector3 reflectDir = Vector3.Reflect (dir , hit.normal);
DrawNewRay( hit.point,reflectDir);
}
}
}
void DrawNewRay(Vector3 originPoint, Vector3 direction){
fwd = transform.TransformDirection(direction);
Debug.DrawRay(originPoint, fwd * 50, Color.green);
}
}
Answer by Duke_Hast_Mich · Apr 27, 2017 at 04:38 PM
You aren't even going to get that in real physics. It may be mostly the same energy packet but it is modified by and at least partially re-emitted by the reflective surface.
However, despite there being no Corona like functions in the Unity API you can have your script re-emit the ray from it's primary hit location and forward it in a new direction with a little calculation. Don't beat your face on the wall looking for API functions that don't exist, at least not here, yet.
This is something else, but it shows the concepts within the code. The code won't straight up work in Unity. You'll have to construct the missing functions from multiple ray casts. https://coronalabs.com/blog/2013/05/07/physics-raycasting-and-reflection/
Thank you! it's not exactly what I was looking for but it helps alot
Answer by PankajOnUnity · Apr 25, 2018 at 05:51 PM
I found solution about it https://youtu.be/xv5K8oIDhwc
Your answer
Follow this Question
Related Questions
Best way to get a thickness of a wall between 2 points 2 Answers
Physics.Raycast Not Working... Kinda 0 Answers
Adding MaxDistance to RayCast Stops Raycast From working 0 Answers
2D raycast not working 0 Answers