簡體   English   中英

我應該如何在 InputManager 中配置 Sprint?

[英]How should I configure the Sprint in the InputManager?

此腳本附加到 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”:編輯 > 項目設置 > 輸入:

輸入管理器

我可以將輸入管理器中的大小更改為 19,它會復制取消,因此我將名稱更改為 Sprint。 但是 Sprint 的配置應該是什么? 現在是取消配置。

輸入管理器

運行游戲時,我收到此異常:

ArgumentException:未設置輸入按鈕 Sprint。 要更改輸入設置,請使用:編輯 -> 項目設置 -> 輸入 PlayerController.Update ()(在 Assets/My Scripts/Character1/PlayerController.cs:62)

這聽起來可能很傻,但你保存了你的項目嗎? 如果您保存場景,這與保存項目設置所在的項目不同。

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

所以這可能很舊,但我將回應那些可能仍在徒勞搜索的人:

將輸入管理器中的輸入添加或更改為“Sprint”時。 即使我為每個正面或負面按鈕甚至是小號放置了正確的命名約定,它也會導致我錯誤地說它沒有設置。

唯一的解決方法是將名稱從“Sprint”更改為“Run”,或者出於任何瘋狂的 Unity 原因,除了“Sprint”之外的任何工作都有效。 希望這可以幫助那些像我第一次遇到這種情況時一樣瘋狂的人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM