【问题标题】:Player movement starts after a few seconds after pressing the button玩家在按下按钮几秒钟后开始移动
【发布时间】:2015-07-27 04:43:36
【问题描述】:

我正在学习 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);
    }
}

我是新人,所以我可能需要更多解释。谢谢大家!

【问题讨论】:

    标签: c# unity3d unityscript unity5


    【解决方案1】:

    第一个问题,延迟你的玩家移动,可能是线路造成的

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

    潜在的问题是您正在调用 Time.deltaTime,它旨在在 update() 调用中使用。您的函数在 fixedUpdate() 内部调用,这意味着您应该使用 Time.fixedDeltaTime

    对于您的第二个问题,大喊大叫,代码似乎很好,问题可能在您的动画树中。检查您在喊叫前的状态是否可以转换为喊叫,并检查是否正确触发。

    【讨论】:

    • 谢谢指出过渡中的问题大喊大叫,我解决了。关于玩家的移动,问题在于勾选了“有退出时间”的转换。无论如何感谢代码提示,我认为它更正确。
    猜你喜欢
    • 2022-10-24
    • 2018-06-07
    • 1970-01-01
    • 1970-01-01
    • 2018-12-13
    • 1970-01-01
    • 2016-10-12
    • 2017-09-21
    • 1970-01-01
    相关资源
    最近更新 更多