简体   繁体   English

我怎样才能让我的球员跳起来?

[英]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. 我想让我的角色像在Crossy路上一样跳跃。 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 . 使用ForceMode Impulse is great for a jumping character, looks realistic as uses mass. Impulse非常适合跳跃角色,使用质量看起来很逼真。 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. 离开警队模式参数空使Force成为默认为ForceMode这是连续的。 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. 在输入之前检查!jumping可以避免空中跳跃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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