GameObject "child" don't follow "parent".
Simple Ping Pong 2D, I want my ball to stay with platform as long as player won't shoot it and start the game. The problem is when I parent my ballPrefab.transform to empty gameObject which is parented already to platform and have transformation where i want to be it's just don't follow.
public class SpawnBall : MonoBehaviour
{
public Transform ballPointRight;
public Transform ballPointLeft;
private Transform ballPointPosition;
public GameObject ballPrefab;
public static GameObject cloneBall;
public static bool canWeSpawn = true;
// Update is called once per frame
void Update()
{
if(canWeSpawn == true)
{
if (Ball.addPointRight == true)
{
ballPointPosition = ballPointRight;
Ball.addPointRight = false;
}
else
{
ballPointPosition = ballPointLeft;
}
cloneBall = Instantiate(ballPrefab, ballPointPosition.position, ballPointRight.rotation) as GameObject;
cloneBall.transform.parent = ballPointPosition;
canWeSpawn = false;
}
}
}
If someone wins ball spawns on left or right platform, we instantiate ball and then parent it to ballPointPosition where it should be. In unity everything is good the cloneBall (Clone) is parent to ballPointPosition but it's dont follow the platform also ballPointPosition don't follow it they stay together at same position. When i drag them through the scene everything works fine.
Answer by streeetwalker · Apr 15, 2020 at 02:06 PM
@Martines_01, nothing wrong with the code you show, but the question is how are you moving the parent object, and whether or not the physics engine is involved. If the child has a Rigidbody then that is an issue.
That's the player movement code for two platforms : public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour {
public CharacterController2D controller_01;
public CharacterController2D controller_02;
float vertical$$anonymous$$ove_01 = 0f;
float vertical$$anonymous$$ove_02 = 0f;
public float runSpeed = 40f;
// Update is called once per frame
void Update()
{
vertical$$anonymous$$ove_01 = Input.GetAxisRaw("Vertical_01") * runSpeed;
vertical$$anonymous$$ove_02 = Input.GetAxisRaw("Vertical_02") * runSpeed;
}
private void FixedUpdate()
{
controller_01.$$anonymous$$ove(vertical$$anonymous$$ove_01 * Time.fixedDeltaTime, false, false);
controller_02.$$anonymous$$ove(vertical$$anonymous$$ove_02 * Time.fixedDeltaTime, false, false);
}
}
And the ball have rigidbody and walls but the object that is for positioning is an empty object. Also if there is the way to do it withou rigidbody becouse sometimes ball i speeding up or slowing the platforms I don't know how to make this "withous mass" effect that velocity will be always same for the ball and it won't interact with platforms
If the child has a Rigidbody, then is under physical forces and it won't move with the parent - it doesn't matter what the parent is. The physics engine is taking over control of the child.
Remove the physics from the child and place it on the parent, and apply the forces to the parent.
Your answer
Follow this Question
Related Questions
Set parent of an object when they collide? 2 Answers
how add child to multi gameobject 0 Answers
Child transform same as parents transform 1 Answer
How can i add a child to a parrent without changing the transfom of the child ? 0 Answers
Why does disabling an object eliminate its transform performance cost? 1 Answer