converting local space vector into world space?
So, I have a movement script for my character based entirely in the animator controller. The character has a reference vector in front of him to monitor which area is straight ahead of him, then the character's mouse X rotates a second vector for look direction around the character. The angle difference between the reference vector and the look direction vector is the offset angle. Then depending on this offset angle, the blend tree blends between different turn speed animations to turn the character.
For example, if I move my mouse to the right suddenly, the offset angle would increase, the character would rotate towards that, thus closing the distance between the reference vector and the look direction vector, and the character would stop rotating when that offset angle dropped below threshold. But, currently, I have it so the angle between the vectors increases, but when the character rotates, both the reference vector and the look direction vector rotate, so the offset angle never changes and the character continues to rotate until I manually move my mouse to close the gap, because the look direction vector is in local space rather than world space. I'm not quite sure how to convert the vector into world space though to achieve this effect. Here's what I have:
private Animator playerAnim;
public Vector3 refDir;
public Vector3 lookDir;
public int lookSens;
public float yRotation;
public float offsetAngle;
void Start()
{
playerAnim = GetComponent<Animator>();
lookDir = new Vector3(0.0f, 0.0f, 1.0f);
}
// Update is called once per frame
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * Time.deltaTime * lookSens;
refDir = new Vector3(0, 0, 1);
lookDir = Quaternion.Euler(0.0f, mouseX, 0.0f) * lookDir;
offsetAngle = Vector3.Angle(refDir, lookDir);
playerAnim.SetFloat("HorAimAngle", offsetAngle);
}
Also, as a smaller note I might as well throw out there. My offset angle is never negative right now because the vector3.Angle seems to only take the absolute value of that offset, so my character only wants to turn right for now. Thanks in advance
Answer by dkjunior · Oct 12, 2015 at 07:17 PM
Your lookDir is actually in world space. It never changes with character rotation, it only changes in response to mouse movement.
I think the problem is that your refDir vector is constant (always equal to (0,0,1)) while you want it to represent the current character rotation. What does your animation do? Does it rotate the object around Y axis? If so, setting refDir to transform.forward instead of (0,0,1) may help:
refDir = transform.forward;