- Home /
Question about TransformDIrection and Vector3.Dot
I was working on a script for a fighting game to get my players to turn around after landing. I was initially using transform.TransformDirection(Vector3.forward ,as seen in the commented out code, as my lhs. However after passing the target the player would rotate wildly between ( 0, 270, 0) and (0, -90, 0) rather than facing the target's position at what would be the former. I have since fixed this by using global positioning but still am wondering what was causing the character to flip around when using the local direction?
public class TestOrientation : MonoBehaviour
{
public Transform target;
CharacterController _controller;
void Start()
{
_controller = GetComponent<CharacterController>();
}
void Update()
{
// Vector3 forward = transform.TransformDirection(Vector3.forward);
Vector3 toTarget = (target.position - transform.position).normalized;
if (_controller.isGrounded)
{
if (Vector3.Dot(Vector3.right, toTarget) < -0.1)
{
transform.eulerAngles = new Vector3(0, 270, 0);
}
else if (Vector3.Dot(Vector3.right, toTarget) > 0.1)
{
transform.eulerAngles = new Vector3(0, 90, 0);
}
}
}
}
Your answer
Follow this Question
Related Questions
c# modify only one axis of a quaternion 2 Answers
Turn and Move Foward 1 Unit relative to Objects local direction 1 Answer
Object wont rotate in the opposite direction 1 Answer
Euler Angles and Vectors 3 Answers
Transforming slowly 1 Answer