I’m getting results from transform.TransformDirection that aren’t making sense to me. I’ve a sprite with a script attached. I use
facing_worldspace = transform.TransformDirection(transform.up);
Debug.Log ("Aim " +facing_worldspace);
Up is the y vector for my sprite. I can rotate the sprite using controls. When it points up, facing_worldspace = (0,1,0); When I turn anticlockwise 45 degrees, facing_worldspace = (-1,0,0). At 90 degrees, it’s (0,-1,0).
I’m wanting to get the direction of the sprite in world-coordinates (I actually want to apply it to a child sprite, hence I can’t just use the object’s x direction, transform.x).
A simple experiment. Slap this script on any object. The results are wrong, reporting a vector equivalent to twice the degree of rotation.
using UnityEngine;
using System.Collections;
public class test : MonoBehaviour {
void Update () {
Vector3 facing_worldspace;
transform.Rotate(0,0,1);
facing_worldspace = transform.TransformDirection(transform.up);
Debug.Log ("Aim " +facing_worldspace);
}
}