0
\$\begingroup\$

So basically, I was trying to make moving platforms. When I finished my prototype, I noticed that the movement on the platform is very wobbly: https://www.youtube.com/watch?v=hmBumsrlHGA&feature=emb_logo To snap the player onto the platform, I am just making it a child of the platform. Anyway, I don't know what is causing this issue and was wondering if I can get any help. Thank you in advance

Here, the controller code:

private void Movement()
    {
        //Extra gravity
        rb.AddForce(Vector3.down * Time.deltaTime * 10);

        //Find actual velocity relative to where player is looking
        Vector2 mag = FindVelRelativeToLook();
        float xMag = mag.x, yMag = mag.y;

        //Counteract sliding and sloppy movement
        CounterMovement(x, y, mag);

        //If holding jump && ready to jump, then jump
        if (readyToJump && jumping) Jump();

        //Set max speed
        float maxSpeed = this.maxSpeed;
        if (sprinting)
        {
            this.maxSpeed = 25;
        }
        else
        {
            this.maxSpeed = 15;
        }

        if(sprinting && y < 0)
        {
            this.maxSpeed = 15;
        }

        //If sliding down a ramp, add force down so player stays grounded and also builds speed
        if (crouching && grounded && readyToJump)
        {
            rb.AddForce(Vector3.down * Time.deltaTime * 3000);
            return;
        }

        //If speed is larger than maxspeed, cancel out the input so you don't go over max speed
        if (x > 0 && xMag > maxSpeed) x = 0;
        if (x < 0 && xMag < -maxSpeed) x = 0;
        if (y > 0 && yMag > maxSpeed) y = 0;
        if (y < 0 && yMag < -maxSpeed) y = 0;

        //Some multipliers
        float multiplier = 1f, multiplierV = 1f;

        // Movement in air
        if (!grounded)
        {
            multiplier = 0.5f;
            multiplierV = 0.5f;
        }

        // Movement while sliding
        //if (grounded && crouching) { multiplierV = 0.5f; multiplier = 0.5f; }

        //Apply forces to move player
        rb.AddForce(orientation.transform.forward * y * moveSpeed * Time.deltaTime * multiplier * multiplierV);
        rb.AddForce(orientation.transform.right * x * moveSpeed * Time.deltaTime * multiplier);
    }
\$\endgroup\$
4
  • 1
    \$\begingroup\$ Looks like you're missing adding the inertial frame of the object you're travelling with. Forces are in world space, so they don't know anything about parenting. Also, your force calculation is physically incorrect. You're computing a displacement in meters, not a force in Newtons. \$\endgroup\$ – DMGregory Jun 23 '20 at 12:51
  • \$\begingroup\$ How do you apply physics locally? Aren't forces applied respectively to the environment you are in? In this case - the platform \$\endgroup\$ – GoRgo 3 Jun 23 '20 at 15:56
  • \$\begingroup\$ You don't. You take the effects of the local space - the platform - and you add them to your world space effects. \$\endgroup\$ – DMGregory Jun 23 '20 at 15:58
  • \$\begingroup\$ Something like adding additional forces to the player based on the speed and direction of the platform, instead of parenting it to the platform and adding forces in the platform's local space? \$\endgroup\$ – GoRgo 3 Jun 23 '20 at 17:27
0
\$\begingroup\$

I have had a nearly identical issue in the past. I solved it by making my Rigidbody kinematic while it is childed to the platform.

void Start()
    {
        rb = GetComponent<Rigidbody>();
    }
void landOnPlatform()
    {
        rb.isKinematic = true;
    }

This means that you will also have to determine when to turn the physics back on. It also has the major caveat of disabling physics on the Rigidbody while it is on the platform, but for my needs, this did the trick.

\$\endgroup\$
2
  • \$\begingroup\$ So basically, I have to make the player rigidbody kinematic when it lands and make it not kinematic whenever the player moves? How doing this would help? Wouldn't making the rigidbody kinematic mean that I can't apply forces, therefore move on the platform? \$\endgroup\$ – GoRgo 3 Jun 23 '20 at 12:49
  • 1
    \$\begingroup\$ If you want to move by forces, then you need to apply forces based on the platform's movement. If you want the platform to move you by virtue of transform parenting, then you need to set position deltas directly. You can't mix and match, having half your movement due to forces and half due to position deltas. The two paradigms are incompatible. \$\endgroup\$ – DMGregory Nov 20 '20 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.