Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Tonba · Jan 21, 2020 at 07:42 AM · transformmobileparentempty game object

Attaching a game object to player when they touch. Also need it stay in posession of player when he moves.

Hello!

It is an icehockey game and im trying to make the puck attach to players stick when player touches the stick. Now the puck is attaching kinda but is acting wildly going all over the hockey field. The puck should teleport to empty gameobject i created in front of the player and stay there as player moves.

public class Puck : MonoBehaviour {

 public GameObject holdSlot;
 public GameObject puck;
 Vector3 pos;



 void Start()
 {
     holdSlot = GameObject.Find("PuckHolder");
     pos = holdSlot.transform.position;
 }

 // Update is called once per frame
 void Update()
 {
     
 }

 public void OnCollisionEnter(Collision col)
 {
     if (col.gameObject.name == "IceSlyder")
     {

         
        transform.parent = holdSlot.transform;
        transform.position = pos;
         //  HERE I NEED TO MAKE THE OBJECT ATTACH TO MY PLAYERS EMPTY GAME OBJECT "holdSlot"
         //  AND MAKE THE OBJECT A CHILD OF THE EMPTY GAME OBJECT SO IT STAYS IN PLAYERS POSESSION AS PLAYER MOVES

     }
 }

 

}

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

Answer by Ermiq · Jan 21, 2020 at 01:35 PM

First of all, Start() is called only once in the very beginning of the code runtime. When you assign some value to a field in Start() and you don't reassign other values to the field later, it will have the same original value and will never be changed.
The second thing is: the position property of an object represents the object's position related to the world pivot point at a certain particular moment during a runtime. Therefore, if you set pos = holdSlot.transform.position in Start() and you don't update it later, then the pos will always represent that point where holdSlot has been located at the very beginning of the runtime, so the pos is actually useless in your case.


Also, when you attach an object to another object, you shouldn't use a world position vector as a new position of the child (in most cases).
Transforms have two types of position properties:
1. position - represents the offset relative to the world center pivot point;
2. localPosition - represents the object's offset relative to the object's parent.
When the object has no parents, its 'position` and localPosition are the same because the object is considered to be a child of the world scene in this case.
When you set an object to be a child of another object (by myObject.transform.parent = anotherObject.transform), the child's position becomes its localPosition (the engine doesn't change position properties in the parenting method), therefore, if the child (when it wasn't a child yet) had the position = Vector3(1, 0, 0)(1 meter to the right from the world center), then it will be located at the same offset from it's new parent, its local position will be the same 1 meter to the right from the parent's pivot point. And since the engine automatically adjusts all children positions when the parent is moved and/or rotated, the child will always be 1 meter to the right from the parent whenever you move/rotate the parent.


So, to properly put the child right at the parent object position you need to reset its localPosition to Vector3(0, 0, 0) right after the parenting method:

 transform.parent = holdSlot.transform;
 transform.localPosition = Vector3.zero;

Notice that you don't even need the pos at all.

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 Tonba · Jan 22, 2020 at 05:55 AM 0
Share

Thank you so much for your help and helpful reply ! It works now with your help :)

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

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

Offset transform.parent.position - Help 1 Answer

Accessing children of instances vs children of original prefab 1 Answer

Setting parent of instantiated object fails (Error: setting parent of prefab is disabled...) 1 Answer

getting object local direction? 1 Answer

Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption. WHY??? 6 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