Getting a Child back!

Hello folks!

I have an object in my game which is called “Ball”. “Ball” is a child from “Player”.
In my code you press a button and the Ball goes away with force and is in the game. That works!

But i want this whole process make backward. something like:

if (something)
{
go back to your parent;
}

Here is my code:

void Update () {

rb.velocity = rb.velocity.normalized * MaxSpeed;

if (Input.GetButtonDown(“Fire1”) && ballInPlay == false)
{
transform.parent = null;
ballInPlay = true;
rb.isKinematic = false;
rb.AddForce(new Vector3(0, ballInitialVelocity, 0));

}

something like:

Transform originalParent;

void Update()
{
if (Input.GetButtonDown("Fire1") && ballInPlay == false)
{
originalParent=transform.parent;
transform.parent = null;
ballInPlay = true;
rb.isKinematic = false;
rb.AddForce(new Vector3(0, ballInitialVelocity, 0));
}
}

if(something)
{
rb.isKinematic=true;
transform.parent=originalParent;
}

@jaasso :

thx for reeply but all i got from this is compiler error, maybe it is just a little syntax-thing. i dont`t get it :frowning:

@LeftyRighty :

// YES, SIR! :smile:
1 Like

I am not sure what @jaasso is driving at with his codelet, but it has some statements (like the if(something) block) that are located outside of his Update() function, so that won’t work. I think he was just trying to give you some general guidelines, but I’ll let him speak to what he was trying to convey.