简体   繁体   English

动画师 setFloat 不响应 C# Unity3d

[英]Animator setFloat doesn't response C# Unity3d

I'm making 3d animation for charachter movement, and Animator.SetFloat(...) doesn't response in functions that I call in if statements, code below.我正在制作 3d animation 用于字符移动,而 Animator.SetFloat(...) 在我在 if 语句中调用的函数中没有响应,代码如下。 Interesting thing that these Debug.Log's work properly, when I walk console has "Walk", when I run console has "Run" etc. I think I have problem with Scope, but I'm really sure.有趣的是,这些 Debug.Log 可以正常工作,当我走路时控制台有“Walk”,当我运行控制台时有“Run”等。我认为 Scope 有问题,但我很确定。 Screenshots: animator props console logs截图:动画师道具控制台日志

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
    [SerializeField] private float moveSpeed;
    [SerializeField] private float walkSpeed;
    [SerializeField] private float runSpeed;
    [SerializeField] private float groundCheckDistance;
    [SerializeField] private float gravity;
    [SerializeField] private bool isGrounded;
    [SerializeField] private LayerMask groundMask;
    [SerializeField] private float jumpHeight;
    private Vector3 moveDirection;
    private Vector3 velocity;
    private CharacterController controller;
    private Animator anim;
    void Start()
    {
        controller = GetComponent<CharacterController>();
        anim = GetComponentInChildren<Animator>();
    }
    void Update()
    {
        Move();
    }
    private void Move()
    {
        isGrounded = Physics.CheckSphere(transform.position, groundCheckDistance, groundMask);

        if(isGrounded && velocity.y < 0) velocity.y = -2f;

        float moveZ = Input.GetAxis("Vertical");

        moveDirection = new Vector3(0, 0, moveZ);
        moveDirection = transform.TransformDirection(moveDirection);

        if (isGrounded)
        {
            if (Input.GetKeyDown(KeyCode.Space)) Jump(); 

            if (moveDirection != Vector3.zero && !Input.GetKey(KeyCode.LeftShift)) Walk();
            
            else if (moveDirection != Vector3.zero && Input.GetKey(KeyCode.LeftShift)) Run();

            Idle();

            moveDirection *= moveSpeed;
        }
        controller.Move(moveDirection * Time.deltaTime);
        velocity.y += gravity * Time.deltaTime;
        controller.Move(velocity * Time.deltaTime);
    }
    private void Idle() 
    {
        anim.SetFloat("speed", 0);
    }
    private void Walk()
    {
        anim.SetFloat("speed", 0.5f);
        moveSpeed = walkSpeed;
        Debug.Log("Walk");
    }
    private void Run() 
    {
        moveSpeed = runSpeed;
        Debug.Log("Run");
        anim.SetFloat("speed", 1);
    }
    private void Jump()
    {
        velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
        isGrounded = false;
    }
}

Ok, I've found the solution, I missed "else", because of that Function Idle() always had control under Animator.SetFloat(...)好的,我找到了解决方案,我错过了“其他”,因为 Function Idle() 总是在 Animator.SetFloat(...)

if (isGrounded)
        {
            if (Input.GetKeyDown(KeyCode.Space)) Jump(); 

            if (moveDirection != Vector3.zero && !Input.GetKey(KeyCode.LeftShift)) Walk();
            
            else if (moveDirection != Vector3.zero && Input.GetKey(KeyCode.LeftShift)) Run();

            else Idle(); //this how it must looks like

            moveDirection *= moveSpeed;
        }

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

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