与对象碰撞时,Unity弹跳 [英] Unity bouncing when colliding with object

查看:476
本文介绍了与对象碰撞时,Unity弹跳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个带有动作的脚本,除了一个东西外,它最终可以按我的意愿工作……我希望它是第一人称的游戏,但是动作现在的工作方式是全局的,这意味着W总是朝着一个特定的方向旋转,无论我的相机朝哪个方向旋转...我该如何解决?我希望机芯保持原样,但根据相机或播放器的观看方式,始终将W键保持向前。

I have made a script with movement, that finally works as i want it to, except one thing... I want it to be a first person game, but the way the movement works now, is on the global axis, which means that W is always torwards one specific direction, no matter what direction my camera is turning... How do i fix this? i want the movement to stay how it is, but with the W key to always be forward depending on the way the camera or player is looking.

请让我知道我的脚本看起来很编辑,或者至少我需要更改什么部分。

Please let me know how my script would look edited, or atleast what part i have to change.

我还想补充一点,就是我希望能够跳墙,但我不确定如何添加这种行为。

I would also like to add that i would love to be able to do a wall jump, but i am not sure how to add that behavior.

这是我的动作脚本:

public class MovementScript : MonoBehaviour {

    public float speed;
    public float jumpforce;
    public float gravity = 25;

    private Vector3 moveVector;
    private Vector3 lastMove;
    private float verticalVelocity;
    private CharacterController controller;

    // Use this for initialization
    void Start () {
        controller = GetComponent<CharacterController> ();

        //Låser og gemmer musen
        Cursor.lockState = CursorLockMode.Locked;
    }

    // Update is called once per frame
    void Update () {
        //Låser musen op
        if (Input.GetKeyDown ("escape"))
            Cursor.lockState = CursorLockMode.None;

        moveVector = Vector3.zero;
        moveVector.x = Input.GetAxis("Horizontal");
        moveVector.z = Input.GetAxis("Vertical");

        if (controller.isGrounded) {
            verticalVelocity = -1;

            if (Input.GetButton("Jump")) {
                verticalVelocity = jumpforce;
            }

        } else {
            verticalVelocity -= gravity * Time.deltaTime;
            moveVector = lastMove;
        }

        moveVector.y = 0;
        moveVector.Normalize ();
        moveVector *= speed;
        moveVector.y = verticalVelocity;

        controller.Move (moveVector * Time.deltaTime);
        lastMove = moveVector;
    }
}


推荐答案

您需要从本地空间转到世界空间:

You need to go from local to world space:

例如,当您想在x轴上移动时,关于玩家的方位,本地矢量为(1、0, 0),它是从输入轴获取的,但是 CharacterController 需要基于世界的方向(请参见 Move函数的文档

for example when you want to move on the x-axis regarding the player's orientation, the local vector is (1, 0, 0), which you get from your input axis, but the CharacterController need a world based direction (see the doc of the Move function)


更复杂

A more complex move function taking absolute movement deltas.

要获取此信息,请使用 Transform.TransformDirection 像这样

To get this, use Transform.TransformDirection like this

worldMove = transform.TransformDirection(moveVector);
controller.Move (worldMove * Time.deltaTime);

编辑关于释放输入后控制器发生一点移动的问题:

EDIT regarding your issue with the controller moving a bit after releasing the input:

这是因为 GetAxis 为您提供了平滑的值。将 GetAxis 替换为 GetAxisRaw ,它应该可以工作

That's because GetAxis gives you a smoothed value. Replace GetAxis by GetAxisRaw and it should work

这篇关于与对象碰撞时,Unity弹跳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆