- Home /
How to Raycast on 45 Degree?
Hello. I need 2 rays from character to left and the right in 45 Degree? How can i do that? Thanks.
Answer by aldonaletto · Jul 22, 2011 at 05:26 AM
You can derive the +45 and -45 vectors just adding or subtracting transform.forward and transform.right:
var right45 = (transform.forward + transform.right).normalized;
// since transform.left doesn't exist, you can use -transform.right instead
var left45 = (transform.forward - transform.right).normalized;
If you want 90 degrees to the right, just use transform.right. For 90 degrees to the left, you can use -transform.right ("transform.left" doesn't exist in Unity, but in the vector world left is the same as -right)
@aldonaletto how would you use a custom angle, like 37.5 degrees to the left?
var a = yourAngleInDegree * $$anonymous$$athf.Deg2Rad;
var dir = (transform.forward * $$anonymous$$athf.Cos(a) + transform.right * $$anonymous$$athf.Sin(a)).normalized;
an alternative:
var dir = Quaternion.Euler(0,yourAngleInDegree,0) * transform.forward;
var dir = Quaternion.Euler(0,yourAngleInDegree,0) * transform.forward; Failed for me.
var a = yourAngleInDegree $$anonymous$$athf.Deg2Rad; var dir = (transform.forward $$anonymous$$athf.Cos(a) + transform.right * $$anonymous$$athf.Sin(a)).normalized; ^^^^ Worked very well. Thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220612152810im_/https://answers.unity.com/themes/thub/images/avi.jpg)