Skip to content

Instantly share code, notes, and snippets.

@unity3dcollege
Created May 26, 2017 19:25
Show Gist options
  • Save unity3dcollege/86d409c55b8d6af881bc18c6b9f979c4 to your computer and use it in GitHub Desktop.
Save unity3dcollege/86d409c55b8d6af881bc18c6b9f979c4 to your computer and use it in GitHub Desktop.
using UnityEngine;
public class Character : MonoBehaviour
{
[SerializeField]
private float moveSpeed = 1f;
private State currentState;
private void Start()
{
SetState(new ReturnHomeState(this));
}
private void Update()
{
currentState.Tick();
}
public void SetState(State state)
{
if (currentState != null)
currentState.OnStateExit();
currentState = state;
gameObject.name = "Cube - " + state.GetType().Name;
if (currentState != null)
currentState.OnStateEnter();
}
public void MoveToward(Vector3 destination)
{
var direction = GetDirection(destination);
transform.Translate(direction * Time.deltaTime * moveSpeed);
}
private Vector3 GetDirection(Vector3 destination)
{
return (destination - transform.position).normalized;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment