Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Catlinman · Oct 08, 2012 at 02:32 PM · transformpositionvector3

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
4

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
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Catlinman · Oct 08, 2012 at 03:00 PM 0
Share

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.

avatar image sparkzbarca · Oct 08, 2012 at 03:00 PM 0
Share

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.

avatar image Catlinman · Oct 08, 2012 at 03:10 PM 0
Share

I actualy want to make the object move freely on its x axis ins$$anonymous$$d of constantly adding a certain movement to it.

avatar image sparkzbarca · Oct 08, 2012 at 10:51 PM 0
Share

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?

avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Catlinman · Oct 09, 2012 at 05:26 PM 0
Share

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.

avatar image Owen-Reynolds · Oct 09, 2012 at 11:20 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do you get the vector3 of one object and assign that vector3 to another object? 1 Answer

How to smooth movement object in one-axis? 1 Answer

how do i move ludo token to right side 0 Answers

Why are my player's positions not being set correctly? 1 Answer

NGUI - Vector 3 Position different from position 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges