I'm following a YouTube tutorial and can't figure out for the life of me why this bug is happening.
Edit: In Visual Studio it says no issues found but in Unity it still shows this same bug. Maybe it's a compatibility issue and I need to fix it somehow? Not sure.
Here's the bug that's been happening:
Assets\AnimatorHandler.cs(73,18): error CS1061: 'AnimatorHandler' does not contain a definition for 'SetFloat' and no accessible extension method 'SetFloat' accepting a first argument of type 'AnimatorHandler' could be found (are you missing a using directive or an assembly reference?)
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
}
}
And here's the tutorial I've been following:
https://www.youtube.com/watch?v=LOC5GJ5rFFw&list=PLD_vBJjpCwJtrHIW1SS5_BNRk6KZJZ7_d&index=7
I tried making sure that all of the names for the AnimatorHandler were spelled correctly and cased correctly but I still can't find out what the problem is. I've also watched the video multiple times to make sure it wasn't something obvious that I was missing but I still can't find anything wrong with the code compared to the tutorial.
CodePudding user response:
You probably want to get the Animator
inside of AnimatorHandler instead of another AnimatorHandler.
public class AnimatorHandler : MonoBehaviour
{
public AnimatorHandler anim; // <-- this should be an animator
eg.
public class AnimatorHandler : MonoBehaviour
{
public Animator anim;