标签:
1 using UnityEngine; 2 using System.Collections; 3 4 public class troll : MonoBehaviour 5 { 6 7 private bool isIdel=true; 8 public float timer = 2; 9 public int speed = 1; 10 private Rigidbody rigidbody; 11 private Animator anim; 12 13 14 15 void Start () 16 { 17 rigidbody = this.GetComponent<Rigidbody>(); 18 anim = this.GetComponent<Animator>(); 19 } 20 21 // Update is called once per frame 22 void Update () 23 { 24 timer -= Time.deltaTime; 25 if (timer<=0) 26 { 27 if (isIdel) 28 { 29 //行走方法 30 TransformToWalk(); 31 } 32 else 33 { 34 35 //进行站立状态 36 TransformToIdel(); 37 } 38 } 39 if (!isIdel) 40 { 41 //进行位移 42 //transform.position += transform.forward*Time.deltaTime*speed; 43 rigidbody.position += transform.forward*Time.deltaTime*speed; 44 } 45 46 } 47 48 public void TransformToIdel() 49 { 50 timer = 2f; 51 isIdel = true; 52 AnimationToIdel(); 53 } 54 55 public void TransformToWalk() 56 { 57 isIdel = false; 58 timer = 5f; 59 int random = Random.Range(-90, 90); 60 transform.Rotate(new Vector3(0,random,0)); 61 AnimationToWalk(); 62 } 63 64 public void AnimationToWalk() 65 { 66 anim.SetFloat("walk",1.0f); 67 anim.SetFloat("idle",0f); 68 anim.SetFloat("run",0f); 69 } 70 71 public void AnimationToIdel() 72 { 73 anim.SetFloat("walk", 0f); 74 anim.SetFloat("idle", 1.0f); 75 anim.SetFloat("run", 0f); 76 } 77 }
标签:
原文地址:http://www.cnblogs.com/fuperfun/p/5402767.html