簡體   English   中英

按下按鈕幾秒鍾后,玩家開始移動

[英]Player movement starts after a few seconds after pressing the button

我正在學習有關Unity 5的教程,以創建一個簡單的隱形游戲。

我按照說明創建了控制播放器運動的腳本。 當我測試游戲時,我注意到玩家按下按鈕后需要幾秒鍾 才能移動

好像在移動之前應該等待Quaternion.Lerp執行的旋轉的結論。

同時按下x按鈕也應引起尖叫,以引起注意並進行適當的動畫處理。它可以發出聲音,但動畫沒有完成 ..在我進行的多次測試中僅執行了一次。

public class PlayerMovement : MonoBehaviour 
{
    public AudioClip shoutingClip;      // Audio clip of the player shouting.
    public float turnSmoothing = 15f;   // A smoothing value for turning the player.
    public float speedDampTime = 0.1f;  // The damping for the speed parameter


    private Animator anim;              // Reference to the animator component.
    private HashIDs hash;               // Reference to the HashIDs.


    void Awake ()
    {
        // Setting up the references.
        anim = GetComponent<Animator>();
        hash = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent<HashIDs>();

        // Set the weight of the shouting layer to 1.
        anim.SetLayerWeight(1, 1f);
    }


    void FixedUpdate ()
    {
        // Cache the inputs.
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        bool sneak = Input.GetButton("Sneak");

        MovementManagement(h, v, sneak);
    }


    void Update ()
    {
        // Cache the attention attracting input.
        bool shout = Input.GetButtonDown("Attract");

        // Set the animator shouting parameter.
        anim.SetBool(hash.shoutingBool, shout);

        AudioManagement(shout);
    }


    void MovementManagement (float horizontal, float vertical, bool sneaking)
    {
        // Set the sneaking parameter to the sneak input.
        anim.SetBool(hash.sneakingBool, sneaking);

        // If there is some axis input...
        if(horizontal != 0f || vertical != 0f)
        {
            // ... set the players rotation and set the speed parameter to 5.5f.
            Rotating(horizontal, vertical);
            anim.SetFloat(hash.speedFloat, 5.5f, speedDampTime, Time.deltaTime);
        }
        else
            // Otherwise set the speed parameter to 0.
            anim.SetFloat(hash.speedFloat, 0);
    }


    void Rotating (float horizontal, float vertical)
    {
        // Create a new vector of the horizontal and vertical inputs.
        Vector3 targetDirection = new Vector3(horizontal, 0f, vertical);

        // Create a rotation based on this new vector assuming that up is the global y axis.
        Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);

        // Create a rotation that is an increment closer to the target rotation from the player's rotation.
        Quaternion newRotation = Quaternion.Lerp(GetComponent<Rigidbody>().rotation, targetRotation, turnSmoothing * Time.deltaTime);

        // Change the players rotation to this new rotation.
        GetComponent<Rigidbody>().MoveRotation(newRotation);
    }


    void AudioManagement (bool shout)
    {
        // If the player is currently in the run state...
        if(anim.GetCurrentAnimatorStateInfo(0).nameHash == hash.locomotionState)
        {
            // ... and if the footsteps are not playing...
            if(!GetComponent<AudioSource>().isPlaying)
                // ... play them.
                GetComponent<AudioSource>().Play();
        }
        else
            // Otherwise stop the footsteps.
            GetComponent<AudioSource>().Stop();

        // If the shout input has been pressed...
        if(shout)
            // ... play the shouting clip where we are.
            AudioSource.PlayClipAtPoint(shoutingClip, transform.position);
    }
}

我是團結的新手,所以我可能需要更多說明。 謝謝大家!

第一個問題,延遲播放器的運動,可能是因為線路

anim.SetFloat(hash.speedFloat, 5.5f, speedDampTime, Time.deltaTime);

潛在的問題是您正在調用Time.deltaTime ,它打算在update()調用中使用。 您的函數在fixedUpdate()內部被調用,這意味着您應該使用Time.fixedDeltaTime

對於第二個問題,大喊大叫,代碼看起來不錯,並且該問題很可能在動畫樹中。 在喊叫之前,請檢查您所處的狀態是否可以轉換為喊叫,然后檢查觸發是否正確。

暫無
暫無

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

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