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 Hamesh81 · Oct 31, 2013 at 01:43 AM · positionaxisalignmatchlocal axis

How can I match/align two objects along 1 axis only?

alt text

In the diagram above 3 characters (purple, orange & blue) are standing on a box. The dashed white line represents the x axis of the box running the full length of the box. Using OnCollisionStay, I would like to smoothly align (not snap) a character standing anywhere on the box, so that their centre is aligned with the dashed line (centre of the box) but to only align the character along their x axis (transform.right). In other words the character would only adjust their sideways position (x axis) not forward or up. Eg. if the character is left of the line/centre (purple character) they would smoothly move to their right until on the line; if the character is to the right of the line (orange character) they would smoothly move to their left. Therefore at the end of the alignment the character would always be in the position that the blue character is in (ignoring the difference along the z axis).

I'm not concerned with which way the character is facing since I already have a script to rotate them in the correct direction, I'm only concerned with the position. I've tried using Vector3.MoveTowards and Vector3.Lerp but I'm not sure how to only use this for the one axis. Any suggestions for this please?

diagram.jpg (24.6 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by robertbu · Oct 31, 2013 at 04:14 AM

As I read through your question, I can interpret what you want in a few different ways. Some simpler, some harder. I'm going to pick a middle of the road solution. It allow for the base box to be at any arbitrary angle on the 'Y', but assumes that it is level with respect to the XZ plane. It also assumes that you might be rotating the character at the same time you are bringing the characters in line.

The solution is to find the closest point on the Vector3.right of the base box to a character. Then the script brings that point up on the 'Y' to the level of the character. This point is then used for a Lerp(). I also added bringing the character into rotational alignment.

Put the following script on each character. Then you need to drag and drop the base cube onto the 'baseBox' variable for each character. Or you could do a simple rewrite and have this script find the base cube using GameObject.Find().

 #pragma strict
 
 var baseBox : Transform;
 var speed = 2.0;
 var rotSpeed = 1.0;
 
 function Update() {
 
     // Find the closest point on the transform.right of the base box
     var v3 = transform.position - baseBox.position;
     var t = Vector3.Dot(v3, baseBox.right);
     var pt = baseBox.position + baseBox.right * t;
     
     // Bring the point up to the level of this game object
     pt.y = transform.position.y;
     
     transform.position = Vector3.Lerp(transform.position, pt, speed * Time.deltaTime);
     
     // Rotation
     var q = Quaternion.LookRotation(baseBox.right);
     transform.rotation = Quaternion.Slerp(transform.rotation, q, rotSpeed * Time.deltaTime);
 }
Comment
Add comment · Show 1 · 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 Hamesh81 · Oct 31, 2013 at 04:35 AM 0
Share

Omg, I just finished my solution and I was about to post it when I saw your answer :O . Haha, I wish I noticed it 20 $$anonymous$$s ago. Anyway my solution is very similar, I ended up using a Vector3.Lerp to adjust the position but also changed the "To" vector so that its y position and x position weren't affected since I needed movement on only one axis. I can see you've also added a slerp for rotation which isn't actually necessary because as I mentioned I've already got the rotation part finished (previous question I posted). Thanks for your help, I'll mark your answer as accepted so you get kudos for your effort ;)

avatar image
0

Answer by FirePlantGames · Oct 31, 2013 at 02:35 AM

have you tried(i don't know if this will work): transform.position.x = Vector3.moveTowards * (a float or int variable)?

Comment
Add comment · Show 1 · 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 Hamesh81 · Oct 31, 2013 at 03:02 AM 0
Share

I don't think you can modify transform.position.x directly, you have to store it as a temp variable first. I'll try that and see if it works.

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

16 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 avatar image avatar image avatar image avatar image

Related Questions

Aligning objects on their relative plane. 1 Answer

How to Lerp a position along one axis only? 2 Answers

How can I rotate towards/look at a specific axis of a collided object? 2 Answers

GUI Button and a gameObject's position 3 Answers

Align X axis of one object according to another 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