using UnityEngine;
using System.Collections;
public class Troll : MonoBehaviour {
public bool idle = true;
public float timer = 2.0f;
public float speed = 5.0f;
private Animator anim;
private CharacterController controller;
private float angle = 0;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
controller = this.GetComponent<CharacterController>();
}
// Update is called once per frame
void Update () {
//Vector3 forward = transform.TransformDirection(Vector3.forward);
timer -= Time.deltaTime;
if (timer <= 0)
{
if (idle)
{
transformToWalk();
}
else {
transformToIdle();
}
}
if (!idle)
{
if (Mathf.Abs(angle) >= 0.2f)
{
float temp = angle * 0.05f;
transform.Rotate(new Vector3(0, temp, 0));
angle -= temp;
}
//transform.position += transform.forward * Time.deltaTime * speed;
controller.SimpleMove(transform.forward * speed);
}
}
public void transformToWalk()
{
idle = false;
timer = 5.0f;
angle = Random.Range(-90, 90);
//int temp = Random.Range(-90, 90);
//transform.Rotate(new Vector3(0, angle, 0));
animationToWalk();
}
public void transformToIdle()
{
idle = true;
timer = 2.0f;
animationToIdle();
}
public void animationToWalk()
{
anim.SetFloat("run", 0.0F);
anim.SetFloat("idle", 0F);
anim.SetFloat("walk", 1.0F);
}
public void animationToIdle()
{
anim.SetFloat("idle", 1F);
anim.SetFloat("walk", 0.0F);
anim.SetFloat("run", 0F);
}
}