我正在關注 YouTube 教程,但我終其一生都無法弄清楚為什么會發生此錯誤。
編輯:在 Visual Studio 中它說沒有發現問題,但在 Unity 中它仍然顯示同樣的錯誤。也許這是一個兼容性問題,我需要以某種方式修復它?不確定。
這是一直在發生的錯誤:
Assets\AnimatorHandler.cs(73,18):錯誤 CS1061:“AnimatorHandler”不包含“SetFloat”的定義,并且找不到接受“AnimatorHandler”型別的第一個引數的可訪問擴展方法“SetFloat”(您是否缺少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 的所有名稱都拼寫正確并且大小寫正確,但我仍然無法找出問題所在。我還多次觀看了視頻,以確保我沒有明顯遺漏,但與教程相比,我仍然找不到代碼有任何問題。
uj5u.com熱心網友回復:
您可能想要獲取 AnimatorHandler 的Animator內部而不是另一個 AnimatorHandler。
public class AnimatorHandler : MonoBehaviour
{
public AnimatorHandler anim; // <-- this should be an animator
例如。
public class AnimatorHandler : MonoBehaviour
{
public Animator anim;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/453186.html
