- Home /
Can someone help me understand world/local space?
Everytime I think I understand it something comes up and I get confused again.
So say I have this code:
var coconutObject : Rigidbody;
var throwForce : float;
function Update () {
if(Input.GetButtonUp("Fire1")){
var newCoconut : Rigidbody = instantiate(coconutObject,transform.position,transform.rotation);
newCoconut.rigidbody.velocity =
transform.TransformDirection(Vector3(0,0, throwForce));
The documentation says it transforms local into world. Alright. But say I'm adding this to an fps controller so and I want it to shoot coconuts in my forward direction. Shouldn't making it into world space, like the TransformDirection does, always make it always go to the WORLD'S z and not the local z? So basically no matter where I turn my character, it should always shoot the coconuts in in the same direction, because they're going forward in the world's z. But it does the opposite! It shoots the coconuts depending on where I turn, but wouldn't that be local z? Because unlike the world z, my local z will always change depending on where i'm turning. Instead, when I DON'T use the TransformDirection, it ALWAYS goes in the same direction. This has been annoying me for a while now and I really want to get this down so I can better understand what i'm doing with unity.
Answer by kramcomposer · Mar 04, 2013 at 08:36 PM
rigidbody.velocity is stored as a World Direction So you Just want to feed it Vector3(0,0,1);
When you use transform.TransformDirection its Asking for a LocalSpace Direction (which you are feeding it a Local Direction of Vector3(0,0,1); and Its converting it into a WorldSpace Direction, basicly, the way your character is Facing.
Another way to put this is~
the Vector3 Struct doesn't know weather its a World Direction, World Position, Local Position, or Relative Direction. it just holds and X,Y, & Z
You have to keep track of what you Vector3 vars are holding...
I hope that makes some sence
And ultimately, just do this:
newCoconut.rigidbody.velocity = new Vector3(0,0, throwForce));
I already understand it now , and honestly, the thing you told me to ultimately do is really bad. Since you're inputting a local variable, when it gets changed into a world variable it will always be the same. So if i turned , the velocity would still be at the same place. So everytime a new coconut gets instantiated it will always shoot the coconut at the same direction no matter where im turning. So in this case transform.TransformDirection(Vector3(0,0, throwForce)); would be better since it transforms the local variable into a world variable for you.
I guess I didn't understand your question very well, I thought you always wanted the Coconut to move in the world direction of Z forward?
If you want the Coconut to move the direction of the way the character is facing, you can just do this.
newCoconut.rigidbody.velocity = transform.forward * throwForce;