• 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
9
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 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 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

GameObject uses his "own" Transform 0 Answers

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

Add force forward to a ball using local coordinates doesn't work as I wanted 1 Answer

Confuse about transform.InverseTransformPoint 1 Answer

transform.position is giving me local space 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges