• 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 Marshal_Alessi · Sep 18, 2019 at 03:12 PM · positionlocalpositiontranform

Setting Position Relative to Object

This seems like it should be simple, so forgive this basic question.

In my game I instantiate four paths which include four starting points for four characters to play. The map is created, and then the players are placed on their respective starting points. I'm able to find where they should be starting, but no matter what way I attempt to set their position/local position, they never even come close to where they should be. My hierarchy for the objects is as follows:

Paths: Scene >> Tiles >> PathA, PathB, PathC, PathD Characters: Scene >> Players >> Player1, Player2, Player3, Player4

In each of the paths, there is an object named "1" which is the transform the player should start at. I've tried the following:

Player1.transform.position = PathA.transform.Find("1").transform.position Player1.transform.localPosition = PathA.transform.Find("1").transform.localPosition Player1.transform.localPosition = PathA.transform.Find("1").transform.position Player1.transform.position = PathA.transform.Find("1").transform.localPosition

Every iteration runs without erroring, but none of them place the character in the correct spot. How can I force the character to have the same position as this transform regardless of the parenting hierarchy?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
-1

Answer by Glurth · Sep 18, 2019 at 03:28 PM

I would suggest you add a field to your Player class:

 public Transform pathStart;

You can set this reference in the editor, or in code, on startup. This will ensure you only need to "find" the transform object once.

Then, when you want to assign the player that position:

 Player1.transform.position = Player1.pathStart.position;

or Create a function in the Player class to do this:

 public void SetToPathStart()
 {
      if(pathStart == null)    
           Debug.LogWarning("PathStart for player "+name+" has not been assigned, before attempting to SetToPathStart().");
      else
          transform.position = pathStart.position
 }

now you can simply call

 Player1.SetToPathStart();

in your game-mechanics/state-machine code.

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 Marshal_Alessi · Sep 18, 2019 at 03:38 PM 0
Share

The player class does store the path, just not the starting point. But doing so would not solve this problem. The problem is that setting the transform does not work as expected.

avatar image Glurth Marshal_Alessi · Sep 18, 2019 at 06:06 PM 0
Share

The pathStart member does NOT store position nor path information...rather it stores a reference to a Transform object, from which you can get a (single/starting) position.

Setting transform.position = pathStart.position WILL assign the object this component is on, the same WORLD position as the pathStart object. If your object has a different position then something else is going on; either you are not assigning the value you expected to (thus my suggestions above on simplifying the code), or the position is being changed after the assignment is done.

avatar image Marshal_Alessi Glurth · Sep 18, 2019 at 09:45 PM 0
Share

If you're curious, I found out this is a bug in Unity. Having trouble finding the thread again but.. It have everything to do with the animator. The transform couldn't be properly set because the pivot of the gameobject reset after the animator completed it's transition. The fix was to instantiate new GameObjects for the player, setting their transform to the starting point, and then reparenting the player.

Regardless, I appreciate your effort. Thank you.

Show more comments

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

136 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 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 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 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

Get instantiated prefab position from Collision Trigger 0 Answers

Camera rotation around player while following. 6 Answers

transform.position not moving object 2 Answers

How to get a vector as if it were a local position to an object? 1 Answer

Setting position on parent transform with many children - performance 0 Answers


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