首页 > 解决方案 > 参数值未从脚本发送到混合树(Unity 2020)

问题描述

(参数值没有从脚本发送到混合树,导致不跟随动画运动。)我在具有一维混合树的混合树中设置了动画。我将其设置为自定义阈值,并以空闲动画作为基本状态,然后混合树具有三个动作:向左、向右和向前移动。当我向前移动时,玩家不会执行树中告诉的动画,我几乎可以肯定它是脚本。

我也在使用 Unity 1212.1.2f1,对于我的脚本,如果重要的话,我一直在使用 Microsoft Visual Studio。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{

    public CharacterController controller;

    public float speed = 12f;
    public float gravity = -9.81f;
    public float jumpHeight = 3f;

    public float rotationSpeed = 75.0f;

    public Animator anim;
    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;

    Vector3 velocity;
    bool isGrounded;
    // Start is called before the first frame update
    void Start()
    {
        anim = GetComponent<Animator>();

    }

    // Update is called once per frame
    void Update()

    {
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
        float translation = Input.GetAxis("Vertical") * speed;
        float rotation = Input.GetAxis("Horizontal") * rotationSpeed;
        translation *= Time.deltaTime;
        rotation *= Time.deltaTime;
        if (isGrounded && velocity.y < 0)

        {
            velocity.y = -2f;

        }
        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        if ((Input.GetButtonDown("Jump")) && isGrounded)
        {

            velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);

        }

        Vector3 move = transform.right * x + transform.forward * z;
        controller.Move(move * speed * Time.deltaTime);

        velocity.y += gravity * Time.deltaTime;

        controller.Move(velocity * Time.deltaTime);


        
        {
            anim.SetFloat("Vertical", Input.GetAxis("Vertical"));
            anim.SetFloat("Horizontal", Input.GetAxis("Horizontal"));
        }

        
    }
}

标签: unity3d

解决方案


首先,我认为您可以尝试制作一个新的 var :

private Rigidbody2D rg;

然后在 Start() 中添加这个

    rg = Player.GetComponent<Rigidbody2D>();

并用这个更新()

anim.SetFloat("Vertical", rg.velocity.x);
anim.SetFloat("Horizontal", rgvelocity.x);

并确保在 Animator 参数中设置了 2 Vertical 和 Horizo​​ntal


推荐阅读