Question by
Josh200016 · Jul 03, 2018 at 07:13 PM ·
animationmovementnewbienpc
NPC Animation
I need some help... I want to make a npc that move with the points i made with empty game objects, I got the movement how I like it, and now I want to animate it, I don't know how to (I'm very new to unity!!). Here is my code, Thanks for the help! Some of the code is from a tutorial, some I learned online...
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class NPC : MonoBehaviour {
[SerializeField]
private float speed;
private Animator animator;
public Transform[] moveSpots;
private int randomSpot;
private float waitTime;
public float startWaitTime;
private int randomStop;
public float Timer = 3.0f;
public float StopTimer = 10f;
private Rigidbody2D myRigidbody;
// Use this for initialization
void Start () {
waitTime -= startWaitTime;
animator = GetComponent<Animator> ();
myRigidbody = GetComponent<Rigidbody2D> ();
randomSpot = Random.Range (0, moveSpots.Length);
}
// Update is called once per frame
void Update ()
{
transform.position = Vector2.MoveTowards (transform.position, moveSpots [randomSpot].position, speed * Time.deltaTime);
if (Vector2.Distance (transform.position, moveSpots [randomSpot].position) < 0.2f) {
if (waitTime <= 0f) {
randomSpot = Random.Range (0, moveSpots.Length);
waitTime -= startWaitTime;
}
else {
waitTime -= Time.deltaTime;
}
}
}
//starts timer
void Awake(){
StartCoroutine (RandomStop());
}
IEnumerator RandomStop()
{
while (true) {
yield return new WaitForSeconds (3);//makes a random stop
randomStop = Random.Range (0, 2);
if (randomStop == 1) {
speed = 0;
StartCoroutine (TimerGo ());
if (randomStop == 0) {
Debug.Log ("NO Random");
}
}
}
}
IEnumerator TimerGo(){//goes after random start
yield return new WaitForSeconds (1);
speed = 3;
StartCoroutine (RandomStop());
StopCoroutine(TimerGo());
}
}
Comment