I just finished level 59 of the Third Person Combat and Traversal course. For some reason my character won’t walk anymore. It plays the walking animation when using the WASD buttons or right joystick, but it won’t move. I can’t figure out why. Does anyone see an issue in my freelookstate code? I can jump and attack just fine but he won’t move.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerFreeLookState : PlayerBaseState // We inherit from PlayerBaseState
{
private const float AnimatorDamptime = 0.12f;
private const float CrossFadeDuration = 0.12f;
private readonly int FreeLookSpeedHash = Animator.StringToHash("FreeLookSpeed");
private readonly int FreeLookBlendTreeHash = Animator.StringToHash("FreeLookBlendTree");
public PlayerFreeLookState(PlayerStateMachine stateMachine) : base(stateMachine)
{}
public override void Enter()
{
stateMachine.InputReader.TargetEvent += OnTarget;
stateMachine.InputReader.JumpEvent += OnJump;
stateMachine.Animator.CrossFadeInFixedTime(FreeLookBlendTreeHash, CrossFadeDuration);
Debug.Log("Free Look State Enter");
}
public override void Tick(float deltaTime)
{
Debug.Log("Free look state ticking");
if(stateMachine.InputReader.IsAttacking)
{
stateMachine.SwitchState(new PlayerAttackingState(stateMachine, 0));
return; // by returning the lines under here won't execute
}
Vector3 movement = CalculateMovement();
stateMachine.transform.Translate(movement * deltaTime);
Move(movement * stateMachine.FreeLookMovementSpeed, deltaTime);
if (stateMachine.InputReader.MovementValue == Vector2.zero)
{
stateMachine.Animator.SetFloat(FreeLookSpeedHash, 0, AnimatorDamptime, Time.deltaTime);
return;
}
stateMachine.Animator.SetFloat(FreeLookSpeedHash, 1, AnimatorDamptime, Time.deltaTime);
FaceMovementDirection(movement, deltaTime);
//Debug.Log("Free Look State Ticking");
}
public override void Exit()
{
stateMachine.InputReader.TargetEvent -= OnTarget;
stateMachine.InputReader.JumpEvent -= OnJump;
Debug.Log("Exiting Free Look State");
}
private void OnTarget()
{
if(!stateMachine.Targeter.SelectTarget()) { return; }
stateMachine.SwitchState(new PlayerTargetingState(stateMachine));
}
private void OnJump()
{
stateMachine.SwitchState(new PlayerJumpingState(stateMachine));
}
private Vector3 CalculateMovement()
{
Vector3 forward = stateMachine.MainCameraTransform.forward;
Vector3 right = stateMachine.MainCameraTransform.right;
forward.y = 0;
right.y = 0;
forward.Normalize();
right.Normalize();
return forward * stateMachine.InputReader.MovementValue.y + right * stateMachine.InputReader.MovementValue.x;
}
private void FaceMovementDirection(Vector3 movement, float deltaTime)
{
stateMachine.transform.rotation = Quaternion.Lerp(
stateMachine.transform.rotation,
Quaternion.LookRotation(movement),
deltaTime * stateMachine.RotationDamping);
}
}
This is my playerstatemachine
`using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerStateMachine : StateMachine
{
[field:SerializeField] public InputReader InputReader {get; private set; }
[field:SerializeField] public CharacterController Controller {get; private set; }
[field:SerializeField] public float FreeLookMovementSpeed {get; private set; }
[field:SerializeField] public float TargetingMovementSpeed {get; private set; }
[field:SerializeField] public Animator Animator {get; private set; }
[field:SerializeField] public float RotationDamping {get; private set; }
[field:SerializeField] public AttackData[] Attacks {get; private set; } // this is an attack array [] so it can store multiple attacks
[field:SerializeField] public Targeter Targeter {get; private set; }
[field:SerializeField] public ForceReceiver ForceReceiver {get; private set; }
[field:SerializeField] public WeaponDamage Weapon {get; private set; }
[field:SerializeField] public Health Health {get; private set; }
[field:SerializeField] public Ragdoll Ragdoll {get; private set; }
[field:SerializeField] public float DodgeDuration {get; private set; } // how long does the doge last
[field:SerializeField] public float JumpForce {get; private set; } // how long does the doge last
[field:SerializeField] public float DodgeLength {get; private set; } //how far does the dodge go
[field:SerializeField] public float DodgeCoolDown {get; private set; } //how far does the dodge go
[field:SerializeField] public float PreviousDodgeTime {get; private set; } = Mathf.NegativeInfinity;
public Transform MainCameraTransform {get; private set; }
private void Start()
{
MainCameraTransform = Camera.main.transform;
SwitchState(new PlayerFreeLookState(this)); // this 'this' refers to the instance we are currently in (PlayerStateMachine)
}
private void OnEnable()
{
Health.OnTakeDamage += HandleTakeDamage; //subscribe to taking damage
Health.OnDie += HandleDie;
}
private void OnDisable()
{
Health.OnTakeDamage -= HandleTakeDamage; // unsubscribe from taking damage
Health.OnDie -= HandleDie;
}
private void HandleTakeDamage()
{
SwitchState(new PlayerImpactState(this));
}
private void HandleDie()
{
SwitchState(new PlayerDeadState(this));
}
public void SetDodgeTime(float dodgeTime)
{
PreviousDodgeTime = dodgeTime;
}
}
`
This is my freelookstate
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerFreeLookState : PlayerBaseState
{
private const float AnimatorDamptime = 0.12f;
private const float CrossFadeDuration = 0.12f;
private readonly int FreeLookSpeedHash = Animator.StringToHash("FreeLookSpeed");
private readonly int FreeLookBlendTreeHash = Animator.StringToHash("FreeLookBlendTree");
public PlayerFreeLookState(PlayerStateMachine stateMachine) : base(stateMachine)
{
}
public override void Enter()
{
stateMachine.InputReader.TargetEvent += OnTarget;
stateMachine.InputReader.JumpEvent += OnJump;
stateMachine.Animator.CrossFadeInFixedTime(FreeLookBlendTreeHash, CrossFadeDuration);
Debug.Log("Free Look State Enter");
}
public override void Tick(float deltaTime)
{
if(stateMachine.InputReader.IsAttacking)
{
stateMachine.SwitchState(new PlayerAttackingState(stateMachine, 0));
return;
}
Vector3 movement = CalculateMovement();
Move(movement * stateMachine.FreeLookMovementSpeed, deltaTime);
if (stateMachine.InputReader.MovementValue == Vector2.zero)
{
stateMachine.Animator.SetFloat(FreeLookSpeedHash, 0, AnimatorDamptime, Time.deltaTime);
}
stateMachine.Animator.SetFloat(FreeLookSpeedHash, 1, AnimatorDamptime, Time.deltaTime);
FaceMovementDirection(movement, deltaTime);
//Debug.Log("Free Look State Ticking");
}
public override void Exit()
{
stateMachine.InputReader.TargetEvent -= OnTarget;
stateMachine.InputReader.JumpEvent -= OnJump;
Debug.Log("Exiting Free Look State");
}
private void OnTarget()
{
if(!stateMachine.Targeter.SelectTarget()) { return; }
stateMachine.SwitchState(new PlayerTargetingState(stateMachine));
}
private void OnJump()
{
stateMachine.SwitchState(new PlayerJumpingState(stateMachine));
}
private Vector3 CalculateMovement()
{
Vector3 forward = stateMachine.MainCameraTransform.forward;
Vector3 right = stateMachine.MainCameraTransform.right;
forward.y = 0;
right.y = 0;
forward.Normalize();
right.Normalize();
return forward * stateMachine.InputReader.MovementValue.y + right * stateMachine.InputReader.MovementValue.x;
}
private void FaceMovementDirection(Vector3 movement, float deltaTime)
{
stateMachine.transform.rotation = Quaternion.Lerp(
stateMachine.transform.rotation,
Quaternion.LookRotation(movement),
deltaTime * stateMachine.RotationDamping);
}
}
I really don’t get why. I checked and made sure it still has a freee look movement speed and a chracter controller…
Anyone who knows what could be causing this?
Thanks!