Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
4
Question by ForbiddenSoul · Jan 10, 2016 at 10:12 PM · scripting problemtransformtransform.positionworldspacelocalspace

How do i move an object relative to another objects axis.

Hi,

I got game object 1 that moves around. Game object 2 is supposed to always be 1 unit behind game object. In a script I need to set object 2's position 1 unit back relative to object 1's local axis, and not world space.

One way was to parent game object 2, however this is not always an option. I'm sure it's really simple, but I just can't wrap my brain around it right now. How do I do this?

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
14
Best Answer

Answer by ForbiddenSoul · Jan 10, 2016 at 10:55 PM

Well silly me try to figure this out for 2 hours, then ask the question and 2 seconds later, BAM! You figure it out. The answer was:

 GameObject.transform.TransformDirection()

For anyone else wanting to do this

you have to set object 2's position = object 1's position (+/-) object 1's position.TransformDirection(the offset you want).

Something Like:

 gameObject2.transform.position = gameObject1.position + gameObject1.TransformDirection(new Vector3(0, 1, -1));

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 sniper02311 · Jan 19, 2019 at 04:39 AM 1
Share

If you wanted to add scale to this also (Confirmed that this does work on my end also), then you can add or subtract based on the world position(x,y,z direction), at the end of it the transforms localScale at that axis.

FOr example. If I made objA move right next to the edge of objB on the front of it, I would say,

 gameObject2.transform.position = gameObject1.position + gameObject1.TransformDirection(new Vector3(0, 0, (gameObject1.localScale.z/2)) + or - gameObject2.transform.localScale.z/2));

Prob need to format it better with parethesis but the idea is to take into account scale of BOTH objects in relation to the direction formula. Hope that helps too for snapping objects and such that do not have a 1:1 ratio of each other.

avatar image
0

Answer by sniper02311 · Jan 19, 2019 at 04:41 AM

Changed gameObject [1] and [2] respectively.

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

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

Related Questions

How do I get the world space up direction for RotateAround? 0 Answers

What is the difference between TransformDirection, TransformVector and TransformPoint since they all accept and return a Vector3 value. 1 Answer

Move gameObject with mouse 1 Answer

World to local Inversion not working as I expect 0 Answers

Confuse about transform.InverseTransformPoint 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