码迷,mamicode.com
首页 > 移动开发 > 详细

3d模式下 让敌人拥有自动移动的AI

时间:2016-04-18 01:04:44      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:

 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 }

 

3d模式下 让敌人拥有自动移动的AI

标签:

原文地址:http://www.cnblogs.com/fuperfun/p/5402767.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!