How to move a child object freely (within), when parented to a new parent Object?

Viewed 192

I am creating a moving platform a player can stand on, the platform is simple, just moves left and right. so what i have done is simply move the player Object and attach it to the platform Object whenever OntriggerEnter2D, and on Exit it just simply gets removed as a child of the platform, simple enough.

The problem, however, is when once player becomes the child of the platform it is nearly imposible to move, i can move just incredibly slow (i have the platform set to trigger and rigid to kinematic) where have i gone wrong and why cant i move normally? Code below if needed, any help is greatly appreciated.

//This is isometric Player movement strait from Unity sources

void FixedUpdate()
{
    Vector2 currentPos = rbody.position;
    float horizontalInput = Input.GetAxis("Horizontal"); //* movementSpeed;
    float verticalInput = Input.GetAxis("Vertical"); // * movementSpeed;
    Vector2 inputVector = new Vector2(horizontalInput, verticalInput);
    inputVector = Vector2.ClampMagnitude(inputVector, 1);
    Vector2 movement = inputVector * movementSpeed;
    Vector2 newPos = currentPos + movement * Time.fixedDeltaTime;
    isoRenderer.SetDirection(movement);
    rbody.MovePosition(newPos);
}

//this is the moving Platform movement

public class Patroll: StateMachineBehaviour {

    GameObject NPC;
    GameObject[] waypoints;
    int currentWP;

    void Awake() {
        waypoints = GameObject.FindGameObjectsWithTag("WayPoint");
    }

    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
        NPC = animator.gameObject;
        currentWP = 0;
    }

    override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
        if (waypoints.Length == 0) return;
        if (Vector2.Distance(waypoints[currentWP].transform.position, NPC.transform.position) < 0.2f) {
            currentWP++;
            if (currentWP >= waypoints.Length) {
                currentWP = 0;
            }
        }

        NPC.transform.localPosition = Vector2.MoveTowards(NPC.transform.position, waypoints[currentWP].transform.position, Time.deltaTime * 1.0f);
    }
}

//and this is the interaction is being handled

public class MovingObject: MonoBehaviour
{
    private void OnTriggerEnter2D(Collider2D other) {
        if (other.transform.gameObject.tag == "Player") {
            other.transform.parent = transform;

            //other.transform.parent.SetParent(transform);
        }

    }
    
    private void OnTriggerExit2D(Collider2D other) {

        if (other.transform.gameObject.tag == "Player")

        {
            other.transform.parent = null;
        }

    }
}
1 Answers

If anyone else comes across this one day, here is my own solution to the problem i had... the problem was in the player movement, the rigid was the big issue as it was getting partially canceled out as it becomes the child of other moving object. so just do this instead...

 rbody.velocity = new Vector2(horizontalInput, verticalInput);
Related