• 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 /
  • Help Room /
This question was closed May 13, 2016 at 03:35 AM by Trevdevs for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Trevdevs · May 13, 2016 at 02:55 AM · c#positioning

Assigning the position of a child to the parents origin not working ?

What i'm trying to do is pickup an item from the world and put in the the player's "hand".

The way i see of doing it is assigning the Item as a child of the players hand. Then setting the position of the Item to 0,0,0 Of the hand <- which is ignores and assigns it to the world space instead.

 //This works and assigns the the Item to a child in the player
 ItemInPlay.transform.parent = Player.transform.GetChild(1).gameObject.transform;
 
 //This doesn't work, when it should be assigning the position to the child in the player.
 ItemInPlay.transform.position = Player.transform.GetChild(2).gameObject.transform.position;
 
 //Things ive tried
 //ItemInPlay.transform.position = new Vector3(0, 0, 0);
Comment
Add comment · Show 1
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 Trevdevs · May 13, 2016 at 03:00 AM 0
Share

Here is the full code

 using UnityEngine;
 using System.Collections;
 
 public class PickupItem : $$anonymous$$onoBehaviour
 {
     public GameObject Player;
     public GameObject ItemInPlay;
 
     private bool IsItemSpawned;
 
     public void Start()
     {
         Player = GameObject.Find("Player");
 
         IsItemSpawned = false;
     }
 
     public void Update()
     {
         RaycastHit hit;
         if(Physics.Raycast(Player.transform.position + new Vector3(0,1,0), Player.transform.TransformDirection(Vector3.forward), out hit))
         {
             if (hit.transform.gameObject.tag == "Items")
             {
                 ItemInPlay = hit.collider.gameObject;
 
                 switch(hit.collider.gameObject.name)
                 {
                     case "Sword":
                     Debug.Log("There is a sword in front of me");
                         ItemInPlay.transform.parent = Player.transform.GetChild(1).gameObject.transform;
                         ItemInPlay.transform.position = new Vector3(0, 0, 0);
                         IsItemSpawned = true;
                     break;
 
                     case "Bow":
                     Debug.Log("There is a bow in front of me");
                     break;
                 }
             }
         }
     }
 }

0 Replies

  • Sort: 

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

155 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

Related Questions

Help! Animating hogs my Pos Y. 0 Answers

Position Child Object to center of Parent Object [SOLVED] 1 Answer

[Solved] Setting position of rectTransform through anchor presets in code 3 Answers

Instantiate position for shooting projectile is way off. Could I get some help? 0 Answers

How can I correctly generate Button objects with C# script? 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