Created
May 26, 2017 19:25
-
-
Save unity3dcollege/86d409c55b8d6af881bc18c6b9f979c4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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