- Home /
Question by
SandorChickane · Oct 26, 2014 at 10:18 PM ·
2dcollisiongameobjecttransform
[2D] Unsure why a gameObject is randomly falling through the floor instead of being transformed to the player
Hey!
I'm very new to Unity, and was testing my limited C# knowledge in 2D to create a simple system to allow the player to 'pick up' a gameobject. This basically attaches the object to the player by transforming it to the player's position with every frame.
For some reason, this works perfectly for about 5 seconds before the object just randomly stops adhering to the player and falls through the floor. I do make the object into a trigger so that its collision doesn't mess with the player, but that shouldn't matter when its position is changed every frame to the player's location, right?
This is the Update code:
void Update ()
{
//if the object is already picked up and the player presses x, drop the object
if (Input.GetKeyDown (KeyCode.X) && isPicked == true)
{
isPicked = false;
this.collider2D.isTrigger = false;
this.transform.localPosition = new Vector2 (Player.transform.localPosition.x + 1, Player.transform.localPosition.y);
} //if the object is not picked up and the player presses x next to it, pick up the object
else if (Input.GetKeyDown (KeyCode.X) || isPicked == true)
{
distance = Vector2.Distance (this.transform.position, Player.transform.position);
if (distance < 2)
{
this.transform.parent = LifeSupport.transform;
this.transform.localPosition = new Vector2 (Player.transform.localPosition.x, Player.transform.localPosition.y);
this.transform.localRotation = Quaternion.identity;
this.collider2D.isTrigger = true;
isPicked = true;
}
}
}
I would much appreciate any help someone could offer :)
Cheers in advance!
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613170025im_/https://answers.unity.com/themes/thub/images/avi.jpg)