So I have been following along the course for the game, adding code and scripts and asking for help when I am in trouble. But this seems backwards. I know my mover script is updated to everything it has been used so far but the code shown in the lecture makes it seem like this is something I should have looked at sooner.
Or could we have used a race condition example with another section of code?
Here is my mover script now as it is currently setup:
``using System;
using System.Collections;
using System.Collections.Generic;
using RPG.Core;
using UnityEngine;
using UnityEngine.AI;
using RPG.Saving;
using RPG.Attributes;
namespace RPG.Movement
{
public class Mover : MonoBehaviour, IAction, ISaveable
{
[SerializeField] Transform target;
[SerializeField] float maxSpeed = 6f;
NavMeshAgent navMeshAgent;
Health health;
// Start is called before the first frame update
void Start()
{
navMeshAgent = GetComponent<NavMeshAgent>();
health = GetComponent<Health>();
}
// Update is called once per frame
void Update()
{
navMeshAgent.enabled = !health.IsDead();
UpdateAnimator();
}
public void StartMoveAction(Vector3 destination, float speedFraction)
{
GetComponent<ActionScheduler>().StartAction(this);
MoveTo(destination, speedFraction);
}
public void MoveTo(Vector3 destination, float speedFraction)
{
navMeshAgent.destination = destination;
navMeshAgent.speed = maxSpeed * Mathf.Clamp01(speedFraction);
navMeshAgent.isStopped = false;
}
public void Cancel()
{
navMeshAgent.isStopped = true;
}
private void UpdateAnimator()
{
Vector3 velocity = navMeshAgent.velocity;
Vector3 localVelocity = transform.InverseTransformDirection(velocity);
float speed = localVelocity.z;
GetComponent<Animator>().SetFloat("forwardSpeed", speed);
}
[System.Serializable]
struct MoverSaveData
{
public SerializableVector3 position;
public SerializableVector3 rotation;
}
public object CaptureState()
{
Dictionary<string, object> data = new Dictionary<string, object>();
data["position"] = new SerializableVector3(transform.position);
data["rotation"] = new SerializableVector3(transform.eulerAngles);
return data;
}
public void RestoreState(object state)
{
Dictionary<string, object> data = (Dictionary<string, object>) state;
navMeshAgent.enabled = false;
transform.position = ((SerializableVector3)data["position"]).ToVector();
transform.eulerAngles = ((SerializableVector3)data["rotation"]).ToVector();
navMeshAgent.enabled = true;
GetComponent<ActionScheduler>().CancelCurrentAction();
}
}
}
``
But at the start of this lesson the code seems to be missing the code that was entered in before. Unless things had adjusted and changed off screen or I missed on removing checks I am wondering what the reason for this was. Or am I just confusing myself?