我应该如何在InputManager中配置Sprint?

时间:2018-05-14 15:35:45

标签: c# unity3d

此脚本附加到Player对象:

using UnityEngine;
using System.Collections;

[RequireComponent (typeof (Rigidbody))]
[RequireComponent (typeof (BoxCollider))]

public class PlayerController : MonoBehaviour {

    public float walkSpeed = 6;
    public float runSpeed = 10;
    public float strafeSpeed = 5;
    public float gravity = 20;
    public float jumpHeight = 2;
    public bool canJump = true;
    private bool isRunning = false;
    private bool isGrounded = false;

    public bool IsRunning
    {
        get { return isRunning; }
    }

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

    void FixedUpdate () {
        // get correct speed
        float forwardAndBackSpeed = walkSpeed;

        // if running, set run speed
        if (isRunning) {
            forwardAndBackSpeed = runSpeed;
        }

        // calculate how fast it should be moving
        Vector3 targetVelocity = new Vector3(Input.GetAxis("Horizontal") * strafeSpeed, 0, Input.GetAxis("Vertical") * forwardAndBackSpeed);
        targetVelocity = transform.TransformDirection(targetVelocity);

        // apply a force that attempts to reach our target velocity
        Vector3 velocity = GetComponent<Rigidbody>().velocity;
        Vector3 velocityChange = (targetVelocity - velocity);
        velocityChange.y = 0;
        GetComponent<Rigidbody>().AddForce(velocityChange, ForceMode.VelocityChange);

        // jump
        if (canJump && isGrounded && Input.GetButton("Jump")) {
            GetComponent<Rigidbody>().velocity = new Vector3(velocity.x, Mathf.Sqrt(2 * jumpHeight * gravity), velocity.z);
            isGrounded = false;
        }

        // apply gravity
        GetComponent<Rigidbody>().AddForce(new Vector3 (0, -gravity * GetComponent<Rigidbody>().mass, 0));
    }

    void Update() {
        // check if the player is touching a surface below them
        checkGrounded();

        // check if the player is running
        if (isGrounded && Input.GetButtonDown("Sprint")) {
            isRunning = true;
        }

        // check if the player stops running
        if (Input.GetButtonUp("Sprint")) {
            isRunning = false;
        }
    }

    void checkGrounded() {
        /* ==============
         * REMEMBER
         * ==============
         * If you change the size of the prefab, you may have
         * to change the length of the ray to ensure it hits
         * the ground.
         * 
         * All obstacles/walls/floors must have rigidbodies
         * attached to them. If not, Unity physics may get
         * confused and the player can jump really high
         * when in a corner between 2 walls for example.
         */
        float rayLength = 0.7f;
        RaycastHit hit;
        Ray ray = new Ray(transform.position, -transform.up);
        //Debug.DrawRay(ray.origin, ray.direction * rayLength);
        // if there is something directly below the player
        if (Physics.Raycast(ray, out hit, rayLength)) {
            isGrounded = true;
        }
    }    
}

脚本中有一些部分正在使用“Sprint” 例如:

// check if the player is running
        if (isGrounded && Input.GetButtonDown("Sprint")) {
            isRunning = true;
        }

        // check if the player stops running
        if (Input.GetButtonUp("Sprint")) {
            isRunning = false;
        }

但是“Spring”没有在编辑器输入中定义: 编辑&gt;项目设置&gt;输入:

Input Manager

我可以将输入管理器中的大小更改为19,它将复制取消,因此我将名称更改为Sprint。但是什么应该是Sprint的配置?它现在是取消配置。

Input Manager

在运行游戏时,我遇到了这个例外:

ArgumentException:未设置输入按钮Sprint。  要更改输入设置,请使用:编辑 - &gt;项目设置 - &gt;输入 PlayerController.Update()(在Assets / My Scripts / Character1 / PlayerController.cs:62)

2 个答案:

答案 0 :(得分:0)

这可能听起来很傻,但是你保存了你的项目吗?如果保存场景,则与保存项目设置所在的项目不同。

File -> Save Scene(CTRL + S)与File -> Save Project

不同

答案 1 :(得分:0)

所以这可能已经很老了,但是我要回应那些仍然徒劳的搜索:

在输入管理器中将输入添加或更改为“ Sprint”时。即使我为每个正向或负向按钮(甚至是alt)都设置了正确的命名约定,也会导致我说没有设置错误。

唯一的解决方法是将名称从“ Sprint”更改为“ Run”,或者出于“疯狂” Unity的原因,除了“ Sprint”之外几乎所有其他可行的方法。希望这可以帮助那些像我初次遇到这种情况时一样发疯的人。