简体   繁体   中英

How can I get my player to jump?

I have been trying to get my player to jump forward for a little while and I am stuck. I have looked at other answers and tried them but then I couldn't get it to work how I wanted it to. I have tried several different ways but none of them are working how they are supposed to. Most of the time the bounces weren't the same distance and sometimes when the player lands on the ground the player will slide forward which is not what I want. I want to have my character jump like in Crossy road. Here is one thing that I tried:

using UnityEngine;
using System.Collections;

public class MovePlayer : MonoBehaviour
{   
    Vector3 endPos;
    int numBackwards = 0;
    bool jumping = false;
    public Rigidbody rigidBody;
    //public Collider theCollider;

    void Start()
    {
        rigidBody = GetComponent<Rigidbody> ();
    }

    // Update is called once per frame
    void Update()
    {
        rigidBody.freezeRotation = true;

        endPos = gameObject.transform.position;
        if (!jumping)
        {
            if (Input.GetButtonDown("up") && gameObject.transform.position == endPos) {
                if (numBackwards < 0)
                {
                    numBackwards++;
                } 
                else
                {
                    UpdateScore.score++;
                }
                transform.Translate(Vector3.up * 50 * Time.deltaTime, Space.World);
                transform.Translate(Vector3.forward * 110 * Time.deltaTime, Space.World);
            } 
            else if (Input.GetButtonDown("down") && gameObject.transform.position == endPos)
            {
                transform.Translate(Vector3.up * 50 * Time.deltaTime, Space.World);
                transform.Translate(-Vector3.forward * 110 * Time.deltaTime, Space.World);
                numBackwards--;
            }
            else if (Input.GetButtonDown("left") && gameObject.transform.position == endPos)
            {
                transform.Translate(Vector3.up * 50 * Time.deltaTime, Space.World);
                transform.Translate(Vector3.left * 110 * Time.deltaTime, Space.World);
            }
            else if (Input.GetButtonDown("right") && gameObject.transform.position == endPos)
            {
                transform.Translate(Vector3.up * 50 * Time.deltaTime, Space.World);
                transform.Translate(Vector3.right * 110 * Time.deltaTime, Space.World);
            }
        }
    }

    void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.CompareTag("Ground"))
            jumping = false;
    }

    void OnCollisionExit(Collision other)
    {
        if (other.gameObject.CompareTag("Ground"))
            jumping = true;
    }
}

This didn't work because almost all of the jumps were different sizes and I need them to be consistent. Another thing I tried was this:

public float distance;      
public float fspeed;
public float uspeed;

//PRIVATE
private Rigidbody rigidBody;


void Awake()
{
    rigidBody = GetComponent<Rigidbody>();
    rigidBody.freezeRotation = true;
}

void FixedUpdate()
{
    if (Physics.Raycast(transform.position, -Vector3.up, distance + 0.1F))
    {
        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            rigidBody.AddForce(new Vector3(0, uspeed, fspeed));
        }

        if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            rigidBody.AddForce(new Vector3(0, uspeed, -fspeed));
        }

        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            rigidBody.AddForce(new Vector3(fspeed, uspeed, 0));
        }

        if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            rigidBody.AddForce(new Vector3(-fspeed, uspeed, 0));
        }
    }
}

This didn't work either and when the player landed it would slide forward. The last thing I tried was using an animation with jump and idle which I couldn't figure out why it wasn't working but that wasn't working either. What I would like to know is what is the best way that I can't get my player to jump the same distance every time without sliding or how can I fix on of my previous ways to make them work how I want it to.

Use a ForceMode . Impulse is great for a jumping character, looks realistic as uses mass. And it's not continuous. Also requires a lower value for more effect.

if (Input.GetKey(KeyCode.UpArrow))
{
   rigidBody.AddForce(new Vector3(0, uspeed, fspeed), ForceMode.Impulse);
}

Leaving the force mode parameter empty causes Force to be default as ForceMode which is continuous. Which causes sliding more than inertia does.

For ignoring inertial sliding nullify velocity when touching ground.

void OnCollisionEnter(Collision other)
{
    if (other.gameObject.tag == "Ground")
    {
        jumping = false;

        rigidBody.velocity = Vector3.zero;
    }        
}

Checking !jumping before input already rules out jumping on air.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM