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 skylerkingdev · Jun 03, 2012 at 03:52 AM · rotationrotateaxismovetranslate

Moving an object based on the position of another object

I want to move an object on its x axis. The object needs to be controlled by a different object that can only move on the Y axis. So for instance I have a cube on the left, it is a rigid body you can only drag up and down on the Y axis. I want the Y axis movement on this object to move a different object on its X axis. The issue that I am having is that the X axis object starts in the wrong position. I want it to start in the position it is in, in the scene, and translate according to movement on the other objects Y axis. Any thoughts?

 function Update () { 
 
     aimObj.position.x = controlObj.position.y * 4;
 
     if (firing == true) {
 
     Fire();
     
     }
     
 
 }
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
0

Answer by MarigoldFleur · Jun 03, 2012 at 06:05 AM

You're directly translating the position of aimObject's x position to controlObj's y position * 4. Instead, first calculate the distance between the two objects and use that as an offset, rather than directly changing it like you are now.

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

Answer by MD_Reptile · Jun 03, 2012 at 05:13 AM

What you need is to relatively move the other object, rather than setting the position to a specific number.

Like sort of like this:

 var moveXAmount : float;
 
 function Update()
     {
     moveXAmount = ObjectThatSlidesUpAndDown.transform.position.y;
     
     ObjectThatMoves.Translate(moveXAmount, 0, 0, Space.Self); // Space.World is default if you dont put the fourth argument, which is absolute rotation
     }

And that should move it based off its local position to begin with, atleast if my memory serves me properly haha...try this and get back if you dont get the desired result.

another thing is, this might continue to move it each frame...not sure exactly how you want the movement to take place, like do you want the moving object to move to a place the slider objects in, and stop there? If so you might take another approach...

To have the slider set the place it will move to, and then smooth the movement to land in a specific spot on the actual object, do something like this:

 function Update()
 {
 desiredPos = ObjectThatSlides.transform.position.y;
 
 currentPos = ObjectThatMoves.transform.position.x;
 
 ObjectThatMoves.transform.position = Vector3.Lerp (currentPos, desiredPos, 0.5f);
 }
Comment
Add comment · Show 3 · 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 skylerkingdev · Jun 03, 2012 at 05:25 AM 0
Share

Well I need the controller that slides up and down to move the object on its x axis, no rotation. So basically its like a slider, up and down movement makes a cube move left to right.

avatar image MD_Reptile · Jun 03, 2012 at 05:27 AM 0
Share

@r6834 oh okay I misunderstood, but... try the Space.Self part, and then try the smoothing the desired movement part, let me know how those work, and I updated my answer to better apply to your question

avatar image skylerkingdev · Jun 03, 2012 at 06:45 AM 0
Share

This solution sends my object flying off to the side. Essentially I just have the 3d up and down slider drag-able to a certain limit. I need this up and down movement to correspond with the cubes left and right translation on the x axis. So by default the cube starts in the center, as does the slider. Sliding up translates the cube left on the x-axis, sliding down translates it right on the x axis. Releasing the slider will bring the cube back to center, as well as the slider back to the center.

This is the code I have on a seperate gameobject that controls the two objects. The slider has its own script.

 var aimObj : Transform;
 var handleObj : Transform;
 var moveXAmount : float;
 
 unction Update () { 
     
     moveXAmount = handleObj.transform.position.y;
     aimObj.Translate(moveXAmount, 0, 0);
     
     if (firing == true) {
 
     Fire();
     
     }
     
 
 }

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Transform.Translate but ignore rotation on one axis 0 Answers

Lock Z Rotation. Character Controller. 1 Answer

Rotating Player 0 Answers

different axis for different objects? 1 Answer

Move and Rotate on a fixed axis? 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