- Home /
How to calculate the Angle of incidence on a plane
I was trying to implement the basic reflection laws in unity using C# . I am not getting as in how to calculate the angle of incidence the light ray(line renderer) , so that i can calculate the angle of reflection . void Update () { LineRenderer lineRenderer = GetComponent<LineRenderer>(); BoxCollider2D boxCollider = GetComponent<BoxCollider2D>(); if(isExpanding) { lineRenderer.SetPosition(0,start); end = new Vector3(start.x+Mathf.Cos(Angle)*i,start.y+Mathf.Sin(Angle)*i,0); lineRenderer.SetPosition(1,end); boxCollider.center = new Vector2(start.x+Mathf.Cos(Angle)*i,start.y+Mathf.Sin(Angle)*i); i = i+0.1f; } void OnTriggerEnter2D (Collider2D collider) { var newline = Instantiate(linePrefab) as Transform; RayScript newlineScript = newline.gameObject.GetComponent<RayScript>(); newlineScript.start = end; //newlineScript.end = end; Debug.Log("watt"); newlineScript.Angle = 2.8f; newlineScript.i = 0.1f; isExpanding = false; return; }
You don't need the angle of incidence. What you need is the inco$$anonymous$$g vector and a vector representing the normal to the surface you want the inco$$anonymous$$g vector to reflect against. If you have both, then you can use Vector3.Reflect() to get the outgoing vector.
any code snippet that you can attach with this as for reference .
listen to what Robertbu is saying and you shouldn't need a snippet, these are the skills you need as a programmer(i understand that you new, im just giving you a criteria to help you). you need to apply any sort of math skills(remember those thing you thought you would never use in real life? well you'll probably use them now!) and logical reasoning to an algorithm that you can convert to code.
get inco$$anonymous$$g vector, you should already have this. if you don't then rethink approach and find a method that nets you the inco$$anonymous$$g vector, you need it to reflect against the normal vector.
get normal vector (hit.normal), this happens after your ray cast, you will need to assign a Raycast hit of course.
uses Vector3.Reflect()
var reflect = Vector3($$anonymous$$yInputVecotr, hit.normal)
what robertbu is saying in short is, get you ray cast vector, get hit.normal and use them in Vector3.Reflect, which will return your desired Vector.
Your answer
Follow this Question
Related Questions
Instantiate Reference Problem 1 Answer
Prefabs wont instantiate using a for loop? 1 Answer
instantiate prefabs/gameObjects 1 Answer