简体   繁体   English

按下按钮几秒钟后,玩家开始移动

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

I'm following a tutorial on Unity 5 to create a simple stealth game. 我正在学习有关Unity 5的教程,以创建一个简单的隐形游戏。

I followed the instructions to create the script that controls the movement of the player . 我按照说明创建了控制播放器运动的脚本。 When I tested the game I noticed that the player takes a few seconds after pressing the button before moving . 当我测试游戏时,我注意到玩家按下按钮后需要几秒钟 才能移动

It's as if before moving should await the conclusion of the rotation that is performed by Quaternion.Lerp . 好像在移动之前应该等待Quaternion.Lerp执行的旋转的结论。

Also pressing the x button should scream to attract attention and take proper animation.. It runs the sound but the animation is not done .. Was performed only once in multiple tests I did. 同时按下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);
    }
}

I'm new in unity so I might need some more explanation. 我是团结的新手,所以我可能需要更多说明。 Thanks to everyone! 谢谢大家!

The first issue, delaying your player movement, may be caused by the line 第一个问题,延迟播放器的运动,可能是因为线路

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

The potential issue is that you are calling Time.deltaTime , which is intended to be use inside an update() call. 潜在的问题是您正在调用Time.deltaTime ,它打算在update()调用中使用。 Your function is called inside of fixedUpdate() which means you should be using Time.fixedDeltaTime . 您的函数在fixedUpdate()内部被调用,这意味着您应该使用Time.fixedDeltaTime

For your second issue, shouting, the code seems fine and the issue is likely in your animation tree. 对于第二个问题,大喊大叫,代码看起来不错,并且该问题很可能在动画树中。 Check that the state you are in before shouting can transition to shout, and checks for the correct trigger. 在喊叫之前,请检查您所处的状态是否可以转换为喊叫,然后检查触发是否正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM