- Home /
Bomb falling from a plane is following the movement of the plane, why?
I've created a plane the is moving on the x axis. I instantiate a bomb falling from the plane so, that the transform.position of the plane equals to the instantiating position of the bomb. Then the bomb, instead of just falling, follows the flane on the x axis while falling. How to make the bomb fall with 0 movement on the x axis? My code:
bombSpawn.position = transform.position;
bool fire = Input.GetKeyDown("space");
if (fire == true && checkBomb == false)
{
bombExisting = Instantiate(Bomb, bombSpawn);
checkBomb = true;
}
Vector3 bombPosition = bombExisting.transform.position;
if (bombPosition.y < -2.8f)
{
Destroy(bombExisting);
checkBomb = false;
}
Because Instantiate( prefab , transform )
is equivalent to:
var go = Instantiate( myPrefab , myTransform );
go.transfrom.parent = myTransform;
Use this instead:
bombExisting = Instantiate( Bomb , bombSpawn.position , bombSpawn.rotation );
Answer by Ambervandinther · May 17, 2021 at 07:59 AM
Is it possible you made the bomb a child of your parent plane?
Answer by Mrpxl · May 17, 2021 at 08:13 AM
Here by instantiating your bomb and setting the second parameter as bombSpawn
, you are setting that bombSpawn as the parent of your bomb. See Transform.parent That is why it will follow the plane since that bombSpawn Transform moves with the plane. What you should do instead is:
bombExisting = Instantiate(Bomb, bombSpawn.position, bombSpawn.rotation);
or:
bombExisting = Instantiate(Bomb);
bombExisting.position = bombSpawn.position;
Here you instantiate the Bomb without setting any parent and it will for sure work now!
Answer by berndtmiklos · May 17, 2021 at 07:01 PM
Thank you! That solved it! I didn't know about parent-child relations between the gameobjects.
Your answer
Follow this Question
Related Questions
zombies and the player SUDDENLY(not a beginning but a middle of playing) falling down 1 Answer
Adding falling and landing animations to the Locomotion System 1 Answer
falling through the terrain 0 Answers
Endless "Falling in place" Simulation Question 0 Answers
Adding Jumping and Falling Animations 0 Answers