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
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
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;
}
}
}
}
}
Follow this Question
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