Unity Game Dev - 'AnimatorHandler' 不包含 'SetFloat' 的定义
Unity Game Dev - 'AnimatorHandler' does not contain a definition for 'SetFloat'
我正在关注 YouTube 教程,但我终究无法弄清楚为什么会出现此错误。
编辑: 在 Visual Studio 中它说没有发现问题,但在 Unity 中它仍然显示相同的错误。也许这是一个兼容性问题,我需要以某种方式修复它?不确定。
这是一直在发生的错误:
Assets\AnimatorHandler.cs(73,18):错误 CS1061:'AnimatorHandler' 不包含 'SetFloat' 的定义并且没有可访问的扩展方法 'SetFloat' 可以找到接受类型 'AnimatorHandler' 的第一个参数(您是否缺少 using 指令或程序集引用?)
AnimatorHandler.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace JM
{
public class AnimatorHandler : MonoBehaviour
{
public AnimatorHandler anim;
int vertical;
int horizontal;
public bool canRotate;
public void Initialize()
{
anim = GetComponent<AnimatorHandler>();
vertical = Animator.StringToHash("Vertical");
horizontal = Animator.StringToHash("Horizontal");
}
public void UpdateAnimatorValues(float verticalMovement, float horizontalMovement)
{
#region Vertical
float v = 0;
if (verticalMovement > 0 && verticalMovement < 0.55f)
{
v = 0.5f;
}
else if (verticalMovement > 0.55f)
{
v = 1;
}
else if (verticalMovement < 0 && verticalMovement > -0.55f)
{
v = -0.5f;
}
else if (verticalMovement < -0.55f)
{
v = -1;
}
else
{
v = 0;
}
#endregion
#region Horizontal
float h = 0;
if (horizontalMovement > 0 && horizontalMovement < 0.55f)
{
h = 0.5f;
}
else if (horizontalMovement > 0.55f)
{
h = 1;
}
else if (horizontalMovement < 0 && horizontalMovement > -0.55f)
{
h = -0.5f;
}
else if (horizontalMovement < -0.55f)
{
h = -1;
}
else
{
h = 0;
}
#endregion
anim.SetFloat(vertical, v, 0.1f, Time.deltaTime);
anim.SetFloat(horizontal, h, 0.1f, Time.deltaTime);
}
public void CanRotate()
{
canRotate = true;
}
public void StopRotation()
{
canRotate = false;
}
}
}
PlayerLocomotion.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace JM
{
public class PlayerLocomotion : MonoBehaviour
{
Transform cameraObject;
InputHandler inputHandler;
Vector3 moveDirection;
[HideInInspector]
public Transform myTransform;
[HideInInspector]
public AnimatorHandler animatorHandler;
public new Rigidbody rigidbody;
public GameObject normalCamera;
[Header("Stats")]
[SerializeField]
float movementSpeed = 5;
[SerializeField]
float rotationSpeed = 10;
void Start()
{
rigidbody = GetComponent<Rigidbody>();
inputHandler = GetComponent<InputHandler>();
animatorHandler = GetComponentInChildren<AnimatorHandler>();
cameraObject = Camera.main.transform;
myTransform = transform;
animatorHandler.Initialize();
}
public void Update()
{
float delta = Time.deltaTime;
inputHandler.TickInput(delta);
moveDirection = cameraObject.forward * inputHandler.vertical;
moveDirection += cameraObject.right * inputHandler.horizontal;
moveDirection.Normalize();
float speed = movementSpeed;
moveDirection *= speed;
Vector3 projectedVelocity = Vector3.ProjectOnPlane(moveDirection, normalVector);
rigidbody.velocity = projectedVelocity;
if (animatorHandler.canRotate)
{
HandleRotation(delta);
}
}
#region Movement
Vector3 normalVector;
Vector3 targetPosition;
private void HandleRotation(float delta)
{
Vector3 targetDir = Vector3.zero;
float moveOverride = inputHandler.moveAmount;
targetDir = cameraObject.forward * inputHandler.vertical;
targetDir += cameraObject.right * inputHandler.horizontal;
targetDir.Normalize();
targetDir.y = 0;
if (targetDir == Vector3.zero)
targetDir = myTransform.forward;
float rs = rotationSpeed;
Quaternion tr = Quaternion.LookRotation(targetDir);
Quaternion targetRotation = Quaternion.Slerp(myTransform.rotation, tr, rs * delta);
myTransform.rotation = targetRotation;
}
#endregion
}
}
这是我一直关注的教程:
https://www.youtube.com/watch?v=LOC5GJ5rFFw&list=PLD_vBJjpCwJtrHIW1SS5_BNRk6KZJZ7_d&index=7
我尝试确保 AnimatorHandler 的所有名称都拼写正确且大小写正确,但我仍然无法找出问题所在。我还多次观看了该视频,以确保我没有遗漏任何明显的东西,但与教程相比,我仍然找不到代码的任何错误。
您可能想要获取 AnimatorHandler 内部的 Animator
而不是另一个 AnimatorHandler。
public class AnimatorHandler : MonoBehaviour
{
public AnimatorHandler anim; // <-- this should be an animator
例如
public class AnimatorHandler : MonoBehaviour
{
public Animator anim;
我正在关注 YouTube 教程,但我终究无法弄清楚为什么会出现此错误。
编辑: 在 Visual Studio 中它说没有发现问题,但在 Unity 中它仍然显示相同的错误。也许这是一个兼容性问题,我需要以某种方式修复它?不确定。
这是一直在发生的错误:
Assets\AnimatorHandler.cs(73,18):错误 CS1061:'AnimatorHandler' 不包含 'SetFloat' 的定义并且没有可访问的扩展方法 'SetFloat' 可以找到接受类型 'AnimatorHandler' 的第一个参数(您是否缺少 using 指令或程序集引用?)
AnimatorHandler.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace JM
{
public class AnimatorHandler : MonoBehaviour
{
public AnimatorHandler anim;
int vertical;
int horizontal;
public bool canRotate;
public void Initialize()
{
anim = GetComponent<AnimatorHandler>();
vertical = Animator.StringToHash("Vertical");
horizontal = Animator.StringToHash("Horizontal");
}
public void UpdateAnimatorValues(float verticalMovement, float horizontalMovement)
{
#region Vertical
float v = 0;
if (verticalMovement > 0 && verticalMovement < 0.55f)
{
v = 0.5f;
}
else if (verticalMovement > 0.55f)
{
v = 1;
}
else if (verticalMovement < 0 && verticalMovement > -0.55f)
{
v = -0.5f;
}
else if (verticalMovement < -0.55f)
{
v = -1;
}
else
{
v = 0;
}
#endregion
#region Horizontal
float h = 0;
if (horizontalMovement > 0 && horizontalMovement < 0.55f)
{
h = 0.5f;
}
else if (horizontalMovement > 0.55f)
{
h = 1;
}
else if (horizontalMovement < 0 && horizontalMovement > -0.55f)
{
h = -0.5f;
}
else if (horizontalMovement < -0.55f)
{
h = -1;
}
else
{
h = 0;
}
#endregion
anim.SetFloat(vertical, v, 0.1f, Time.deltaTime);
anim.SetFloat(horizontal, h, 0.1f, Time.deltaTime);
}
public void CanRotate()
{
canRotate = true;
}
public void StopRotation()
{
canRotate = false;
}
}
}
PlayerLocomotion.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace JM
{
public class PlayerLocomotion : MonoBehaviour
{
Transform cameraObject;
InputHandler inputHandler;
Vector3 moveDirection;
[HideInInspector]
public Transform myTransform;
[HideInInspector]
public AnimatorHandler animatorHandler;
public new Rigidbody rigidbody;
public GameObject normalCamera;
[Header("Stats")]
[SerializeField]
float movementSpeed = 5;
[SerializeField]
float rotationSpeed = 10;
void Start()
{
rigidbody = GetComponent<Rigidbody>();
inputHandler = GetComponent<InputHandler>();
animatorHandler = GetComponentInChildren<AnimatorHandler>();
cameraObject = Camera.main.transform;
myTransform = transform;
animatorHandler.Initialize();
}
public void Update()
{
float delta = Time.deltaTime;
inputHandler.TickInput(delta);
moveDirection = cameraObject.forward * inputHandler.vertical;
moveDirection += cameraObject.right * inputHandler.horizontal;
moveDirection.Normalize();
float speed = movementSpeed;
moveDirection *= speed;
Vector3 projectedVelocity = Vector3.ProjectOnPlane(moveDirection, normalVector);
rigidbody.velocity = projectedVelocity;
if (animatorHandler.canRotate)
{
HandleRotation(delta);
}
}
#region Movement
Vector3 normalVector;
Vector3 targetPosition;
private void HandleRotation(float delta)
{
Vector3 targetDir = Vector3.zero;
float moveOverride = inputHandler.moveAmount;
targetDir = cameraObject.forward * inputHandler.vertical;
targetDir += cameraObject.right * inputHandler.horizontal;
targetDir.Normalize();
targetDir.y = 0;
if (targetDir == Vector3.zero)
targetDir = myTransform.forward;
float rs = rotationSpeed;
Quaternion tr = Quaternion.LookRotation(targetDir);
Quaternion targetRotation = Quaternion.Slerp(myTransform.rotation, tr, rs * delta);
myTransform.rotation = targetRotation;
}
#endregion
}
}
这是我一直关注的教程:
https://www.youtube.com/watch?v=LOC5GJ5rFFw&list=PLD_vBJjpCwJtrHIW1SS5_BNRk6KZJZ7_d&index=7
我尝试确保 AnimatorHandler 的所有名称都拼写正确且大小写正确,但我仍然无法找出问题所在。我还多次观看了该视频,以确保我没有遗漏任何明显的东西,但与教程相比,我仍然找不到代码的任何错误。
您可能想要获取 AnimatorHandler 内部的 Animator
而不是另一个 AnimatorHandler。
public class AnimatorHandler : MonoBehaviour
{
public AnimatorHandler anim; // <-- this should be an animator
例如
public class AnimatorHandler : MonoBehaviour
{
public Animator anim;