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

本文关键字:我的 跳起来 | 更新日期: 2023-09-27 18:03:07

我一直试图让我的球员向前跳一段时间,但我被卡住了。我看过其他答案并尝试过,但后来我无法按照自己的意愿进行。我尝试过几种不同的方法,但都没有达到预期效果。大多数时候反弹的距离不一样,有时当球员落地时,球员会向前滑动,这不是我想要的。我想让我的角色像《十字路口》中那样跳跃。这是我尝试过的一件事:

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

这也不起作用,当玩家落地时,它会向前滑动。我最后尝试的是使用一个带有跳跃和空闲的动画,我不明白为什么它不起作用,但也不起作用。我想知道的是,如果我不能让我的球员每次都跳同样的距离而不滑动,最好的方法是什么,或者我如何修复我以前的方法,使它们按照我想要的方式工作。

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

使用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已经排除了在空中跳跃的可能性。