Can someone explain transform.TransformDirection()? (or how to get facing vector)

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);
	}
}

This code doesn’t really make any sense since transform.up is already relative to the transform. I assume you meant “transform.TransformDirection(Vector3.up);”, but there’s no real reason to use that when you can just use “transform.up” instead, which does the same thing but is a lot shorter.

–Eric

Okay. I thought transform.up etc. was returning the value relative to the parent on account of my angle-finding routine being wonky, hence the attempt to use worldspace transmogrification. A quick reference to the docs would have shown me otherwise! :sweat_smile: