Quaternion and Direction for DrawRay(Make Object Look At Its Feet, Watch where its going...)
I don't actually know how to see the forums of my own questions... (so please email me at: siloaman@hotmail.ca)
My question is really simple.
I have an Avatar that moves across a floor panel maybe 20x20.
I am able to use Debug.DrawRay to watch his Local Forward Direction using:
straightForward = transform.TransformDirection(Vector3.forward) * 10;
What I need to do is create a second DrawRay function that will look in front of him angled towards the ground like he's watching where he is going.
At the start of the game run, my code was:
straightForward = transform.TransformDirection(Vector3.forward) * 10; (Vector3)
noAngle = straightForward; (Vector3)
spreadAngleDown = Quaternion.AngleAxis(15, new Vector3(1,0,0)); (Quaternion)
newVectorDown = noAngle * spreadAngleDown; (Vector3)
if (Physics.Raycast(transform.position, newVectorDown, 10))
{
Debug.DrawRay(transform.position, newVectorDown, Color.green);
Debug.DrawRay(transform.position, straightForward, Color.cyan);
//Debug.Log("Looking at something :)");
onPlatform = true;
}
This code would essentially say that if the Avatar can see something in front of him.. feel free to move ahead. However, when I coded in Input.GetKey(Space) assume its correct and I (transform.Rotate(0, 90, 0, Space.Self);) the Avatar to his right side.. I ran the same 1-4 steps coding again...
Problem is: my DrawRay that looks directly forward is working, but the angled down DrawRay gets wacky, looking upwards and nowhere and random places.. I think it's because its multiplying the Quaternion angles by a new set of world axis values, not Avatar local values.. can someone please help me so that my angled DrawRay always looks at the ground in front of my Avatar local direction???
Please send extra copy of answer to: siloaman@hotmail.ca
Answer by toromano · Dec 14, 2015 at 07:49 PM
@Siloaman I guess you just need another conversion from local to world:
Vector3 straightForward = transform.forward; //forward direction vector in global axis.
Quaternion spreadAngleDown = Quaternion.AngleAxis(15, new Vector3(1,0,0));
Vector3 localDownVector = Vector3.forward * spreadAngleDown;
Vector3 downVector = transform.TransformDirection(localDownVector); // down direction vector in global axis
if (Physics.Raycast(transform.position, downVector, 10))
{
Debug.DrawRay(transform.position, downVector, Color.green);
Debug.DrawRay(transform.position, straightForward, Color.cyan);
//Debug.Log("Looking at something :)");
onPlatform = true;
}
Answer by Siloaman · Dec 15, 2015 at 10:36 PM
Thank you so much!! that totally worked.
Only issue was:
Vector3 localDownVector = Vector3.forward * spreadAngleDown;
but I researched and you have to multiply Quaternion * Vector3
not Vector3 * Quaternion or you get a compiler error :D
I am glad i helped. I did not use a compiler so i could not see that:) BTW, you should not write your comments as answers. Just a friendly advice :)