- Home /
Problem with local position
Hey guys,
I am trying to lock my objects position on its local y and z coordinates letting it still move on its local x axis.
The problem is that when these are used it makes them world coordinates meaning the object doesn't move at all when its local x isn't aligned to the world x axis.
function Start(){
lockedPosition = Vector3(transform.localPosition.x,transform.localPosition.y,transform.localPosition.z);
}
function Update () {
transform.localPosition = Vector3(transform.localPosition.x,lockedPosition.y,lockedPosition.z);
}
Thanks in advance
Answer by Owen-Reynolds · Oct 08, 2012 at 02:42 PM
localPosition gives coordinates in your parent's local space. If you don't have a parent, it's the same as position.
To get your local x-axis (for example,) use `transform.right`, which is a shortcut for `transform.TransformDirection(Vector3.right));` (and `V3.right` is just a shortcut for (1,0,0).) For example:
transform.position += mvAmt * transform.right; // move L/R on my local axis
The problem is I am working with a rigidbody and I need to lock its start y and z so only x is affected. Its supposed to work like freeze position in the rigidbody setting but with the local axis of the object ins$$anonymous$$d of the world axis.
you can also use transform.tranlate(new vector3(movement,0f,0f))
0f just makes a float and prevents change to those axis.
translate automatically moves relative to self so you dont have to call direction.
I actualy want to make the object move freely on its x axis ins$$anonymous$$d of constantly adding a certain movement to it.
Well obviously the easiest solution is to not pass it y,z axis movement but since you want to lock the axis so it doesn't accept that movement it sounds like what you need is to maybe have a general function that deals with input you can assign to an object and deal with movement input.
for example overload the transform.position or have a seperate function used to pass movement into, that takes a vector3 but vector is divided into its arguments. x,y,z
bool y_locked = true;
if (y_locked == true) vector3.y = 0;
that will allow you to pass a normal transform but ignore movement along a "locked" axis by setting the value to 0.
helpful?
Answer by Owen-Reynolds · Oct 09, 2012 at 03:37 PM
If you need it be be a rigidbody, moving on just one arbitrary axis, you could hand-tweak velocity (and pos, maybe) using the "local frame of reference" trick, to only be along your x.
Unity uses world coords for most things. Local coords are for the programmer to "think in." The most common opertations -- `transform.right` or `transform.TransformDirection(V3.right);` convert from local "thinking" coords to useful world coords. To take that one step further, to fix velocity, which is in world coords, convert to useless local coords, fix it to go along the line, then convert back:
// compute local velocity:
Vector3 Lvel = tranform.InverseTransformDirection(rigidbody.velocity);
`Lvel` is still your velocity, but in terms of your personal little red/green/blue arrows (which Unity can't use.) Suppose `Lvel` is `(4,0,1)`. That means your speed is your sideways +4 and your forward +1. You want speed to only be sideways, so kill the y and z:
Lvel.y = Lvel.z = 0;
Now you have velocity in useless local coords, but just convert back the regular way:
rigidbody.velocity = transform.TransformDirection(Lvel);
Or, instead of those two lines, just: `ridigbody.velocity=transform.right*Lvel.x;`
This could go in FixedUpdate (maybe OnCollisionLeave, so you only fix velocity after something changed it.)
Each bump will probably knock your guy a little off his personal L/R axis. A similar trick can put him back. Make an empty to establish the line, subtract and InvTrans to get local, zero out y and z, trans and add to go back.
I am not trying to make an object only receive velocity on one axis(which I actualy already have) but ins$$anonymous$$d keep it from moving on the y and z axis if it is bumped by another rigidbody. This is supposed to apply to a drawer as it only opens in the x direction and is not supposed to move on any other axis.
If you wish to see the old post with the whole function: http://answers.unity3d.com/questions/326248/object-only-moves-on-global-x-axis.html
I just started a new question because I had narrowed the problem down to the locking of the y and z axis of the drawer.
Two ways to say the same thing. If an RB bumps you, it changes your velocity. Either the physics system can "know" to limit to one axis (but appear to be no settings for this,) or you do the velocity limiting math yourself, after each collision.
But for just a drawer - all the ones I know are pretty sticky and ignore most bumps.
Answer by Runalotski · Sep 17, 2014 at 11:21 AM
Hello i have come across velocity.project that is used to make an object act like its on a rail which sounds like what you want.
this is what i followed
http://answers.unity3d.com/questions/303032/make-rigidbody-freeze-to-an-objects-axis.html
and infortation in the script reference
http://docs.unity3d.com/ScriptReference/Vector3.Project.html
i know this is an old post but it might help some else who comes across this thread like me.