unity3d - 参数值未从脚本发送到混合树(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"));
}
}
}
解决方案
首先,我认为您可以尝试制作一个新的 var :
private Rigidbody2D rg;
然后在 Start() 中添加这个
rg = Player.GetComponent<Rigidbody2D>();
并用这个更新()
anim.SetFloat("Vertical", rg.velocity.x);
anim.SetFloat("Horizontal", rgvelocity.x);
并确保在 Animator 参数中设置了 2 Vertical 和 Horizontal
推荐阅读
- swift - Xcode 无法构建新项目 (12.4) - 无法构建 Objective-C 模块“SwiftUI”
- android - android studio - 我在下载时不小心点击了“背景”按钮,现在我找不到下载进度
- mysql - jmerise export 在 mariadb 中导入时出现语法错误,但我在脚本中找不到语法错误
- sql - 递归查询查找满足条件的循环
- python - 有没有办法找到每个预测的特征重要性?
- homebrew - 任何 brew 安装导致'curl:(35)错误:1400410B:SSL 例程:CONNECT_CR_SRVR_HELLO:错误的版本号'
- javascript - 如何在循环中获取动态属性
- python - while 循环,如何设置相等并中断 while 循环?
- c# - 从数据库中检索数据时如何跳过不正确的 DateAndTime 格式
- terraform - 使用 terraform 创建 azure 自动化 dsc 配置和 dsc 配置节点似乎不起作用