如何让我的玩家跳?

时间:2016-08-19 03:39:12

标签: c# unity3d

我一直试图让我的玩家向前跳一会儿而且我被卡住了。我已经查看了其他答案并尝试了它们但是我无法按照我想要的方式工作。我已经尝试了几种不同的方法,但它们都没有按照预期的方式工作。大部分时间弹跳都不是相同的距离,有时当玩家落地时,玩家将向前滑动,这不是我想要的。我想让我的角色像Crossy一样跳跃。这是我尝试过的一件事:

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;
    }
}

这没有用,因为几乎所有的跳跃都是不同的大小,我需要它们保持一致。我尝试的另一件事是:

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));
        }
    }
}

这也没有奏效,当玩家降落时,它会向前滑动。我尝试的最后一件事是使用跳跃和闲置的动画,我无法弄清楚它为什么不起作用,但那也不起作用。我想知道的是,什么是最好的方法,我可以让我的玩家每次跳过相同的距离而不会滑动,或者我怎样才能确定我以前的方式让他们按照我想要的方式工作

1 个答案:

答案 0 :(得分:2)

使用ForceModeImpulse非常适合跳跃角色,使用质量看起来很逼真。而且它不是连续的。还需要较低的值才能获得更好的效果。

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

将强制模式参数保留为空会导致Force默认为ForceMode,这是连续的。这导致滑动而不是惯性。

在接触地面时忽略惯性滑动无效速度。

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

        rigidBody.velocity = Vector3.zero;
    }        
}

在输入之前检查!jumping已经排除了跳跃。