Home > Back-end >  Standing on moving platform disables movement
Standing on moving platform disables movement

Time:06-04

In my game i have moving platforms that are controlled by Vector3.moveTowards. They work just fine And i have a player script that has controlls made like that:

void Update()
    {
        var movement = Input.GetAxis("Horizontal");
        rigidBody.position  = new Vector2(movement, 0) * movementSpeed * Time.deltaTime;
    }

This of course isn't everything in the player controlls, but nothing else does anything that changes position/collider/rigidbody. What i have works just fine The problem is that when i jump on the platform the player moves with platform, but can't move on itself I need to jump to be able to move again only in the air and on the not moving ground

private void OnCollisionEnter2D(Collision2D collision)
    {
        collision.transform.SetParent(transform);
    }
 
    private void OnCollisionExit2D(Collision2D collision)
    {
        collision.transform.SetParent(null);
    }

I tried making movement with addforce and movePosition but had similiar results to the problem i have now but it also happened on not moving ground Here is the rigidbody component of the platform and player's components related to this question

If the jump is important then here is script for it

void Jump()
{
    rigidBody.velocity = new Vector2(rigidBody.velocity.x, 0);
    rigidBody.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);
    Debug.Log("jump");
}

enter image description here enter image description here

CodePudding user response:

I rebuilt your setup with exactly those scripts you posted here, but everything seems to work well for me. My guess is that something goes wrong when you set the parent objects. Since everything looks alright with your trigger and collission setup, I would suggest the following:

  • Try manipulating the left and right going with velocity instead of position as well, since that seems the be the only visible difference between jump and movement that I can see right now.
  • Could it be that you are grabbing the wrong rigidbody component when trying to move while on the platform? (For example, you're grabbing the one of the platform instead of the one of the player?)

I hope that helped in some way! (I would have posted it as a comment, but I dont have enough rep yet)

  • Related