Search code examples
c#unity-game-enginemobilegame-development2d-games

How to make the player jump to the Y axis


so I have a player that I can tilt with a slider. So the more you pull the slider to the right, the more he will rotate to the right till he reaches the limit of 50 degrees, and the same thing for left.

So my question is, how do I make a UI button which will make the player jump to the direction he is rotated to, and make him stop where he landed. Also with ground check of course, so the player won't jump multiple times in the air

I already tried a code, but it made the player continue sliding to the direction he jumped to when he was on the ground.

Also I'm new to unity, so it would be great if you would give me an example code or at least good tutorials that would help me out.

EDIT: I was informed that my question is too broad, so here some more information.

I used the code Vector2 direction = transform.up; to calculate the y direction and then rb.AddForce(direction * jumpForce, ForceMode2D.Impulse); to jump to the calculated direction.

The issue with this code was, that the player kept sliding on the ground after the landing, so in another question someone suggested me to increase ground friction and gave me this code:

private void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.gameObject.CompareTag("Ground"))
    {
        Vector2 counterForce = -rb.velocity.normalized * jumpForce / 2f;

        rb.AddForce(counterForce, ForceMode2D.Impulse);

        rb.velocity *= groundFriction;
    }
}

But unfortunately this code didn't work either, I would say it made it even worse. The player started sliding in the opposite direction and slide really fast and far away.

So, does someone know a better code or a way to fix this code?


Solution

  • Here are some steps you can take to implement jumping mechanics with a button.

    1. Make a new public function (For example public void test (){}).
    2. Add a rigidbody2d to the player game object
    3. In the script, refrence the rigidbody2d by writing "public RigidBody2d rb" and dragging the player game object to the field that pops up in the script.
    4. In the public function you made, write rb.AddForce(transform.up * [jumpForceNumber]);.
    5. Make an if statement using the ground check part of the code (assuming it works), to make it so that even if the function is called, the player has to be touching the ground.
    6. Attach the script to the player, go to your button in the inspector, scroll down in the inspector, and locate the OnClick function. Click the plus icon, attach your player game object, find the name of your player script in the field that pops up, then select your new public function.

    FOR WHERE YOU SAID THE PLAYER "SLIDES":

    In your rigidbody2d, try playing around with the linear drag (increasing it) instead of applying a counterforce. Linear drag, the way unity defines it, is the tendency of an object to slow down due to friction with the air or water that surrounds it. If this doesn't work, it might be the issue of the rigidbody2d as a whole, because the way it works is that you have more mass in the direction you are rotated to, but since I assume you froze the rotational constraints on your rigidbody2d, it is struggling to respond by falling on it is side, so its attempt to move as an alternate.

    AN ALTERNATE ROUTE:

    1. Create an empty game object, and parent the player to it

    2. remove the player controller script, collider, and rigidbody2d from the original player and add them to the new parent.

    3. Make a new script called "RotationHandler" and move the rotation part of the playercontroller to the new script. Attach the new script to the original player and connect it to the slider.

    4. Write this in playercontroller: "private Vector3 playerRotation" in the beginning. Then in the update() function write

      playerRotation = GameObject.Find("OriginalPlayerNameHere").transform.eulerAngles

    5. Go back to where you wrote "rb.AddForce(transform.up * [jumpForceNumber]);" and replace "transform.up" with "playerRotation"

    Hope this helps