Following a YouTube tutorial and no one in the comments has a solution for this problem.
Edit: In Visual Studio the scripts say no issues found but then in Unity I still get this bug. Maybe it’s a compatibility issue and I need to fix it somehow? Not sure but if someone could shine some light on this that would be cool.
At around 25:11 are the two lines that my code doesn’t like
Been trying to fix this bug for the past day but I have literally no idea what’s wrong. Here’s the error message:
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?)
At line 73 and 74 in AnimatorHandler.cs I know that but I can’t figure out why it’s giving me the error when I have watched the video 5 times to figure out what’s wrong.
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
}
}