- Home /
Raycast between TransformDirection and Vector3.Forward?
Hello,
I'm having a bit of a coding/transform/vector issue, which I can't my head around.
I'm currently firing a raycast from the direction my object is facing:
Vector3 _dir = transform.TransformDirection(Vector3.forward);
Works fine, but I would like to add in an 'adjustment' so I can range between the TransformDirection, and a World Forward (Vector3.forward) - and range between those values:
Vector3 _dir = Vector3.forward;
For example, if I have my 'adjustment range' at 0%, the raycast will point only in Vector3.foward World Space, but if I change it to 100%, the raycast will point in the TransformDirection (direction of transform).
Thus, if I have the range at 50% the raycast will be in between the values of Vector3.forward and TransformDirection.
I have no idea how to even code this up though? I can't get my head around it :/
A point in the right direction would be much appreciated (no pun intended). Thanks.
Answer by Bunny83 · Nov 25, 2016 at 01:21 PM
Uhm so you want to slerp between world forward and local forward? Just do this:
Vector3 _dir = Vector3.Slerp(Vector3.forward, transform.forward, percent);
Note that "percent" are "programmer-percent" (aka in the range of 0 to 1 ^^) This might be a bit misleading but using something like "perunum" would be even more confusing as nobody speaks latin :)
ps:
tranform.forward
is exactly the same as
transform.TransformDirection(Vector3.forward)
Thanks for that - I got it working, almost identical to yours. But I'm using Lerp, ins$$anonymous$$d of Slerp. What would be the difference in this case?
Thanks.
Your answer
Follow this Question
Related Questions
Trying to use transform.forward properly 1 Answer
teleport player in the direction they are looking, 1 Answer
InverseTransformDirection and TransformDirection 1 Answer
Declaring Vector in inspector for directional movement 1 Answer
Finding difference between gameObject's x coordinate according to one object's transform 1 Answer