找回密码
 立即注册
查看: 5920|回复: 85

[脚本] 简单的坦克大战AI的脚本

[复制链接]
发表于 2014-10-15 17:58 | 显示全部楼层 |阅读模式
using UnityEngine;
using System.Collections;

public class NPC : MonoBehaviour {
       
        GameObject tank;
        public Transform target;
        public Transform targetRotation1;
        public Transform targetRotation2;
        int cun_time;
       
        void Start()
        {
                cun_time = 0;
                if (target == null && GameObject.Find("NPC"))
                {
                        target = GameObject.Find("NPC").transform;

                }
        }
               
        void Update()
        {       
                tank = GameObject.Find("Player");//实例化
                if (target == null)
                        return;

                Vector3 relativePos1 = target.position - tank.transform.position;
                Quaternion targetRotation1 = Quaternion.LookRotation(relativePos1);//求出与目标之间的角度

                tank.transform.rotation = Quaternion.Slerp(tank.transform.rotation, targetRotation1, Time.deltaTime * 2.0f);//旋转之角度与目标对齐
               
                Vector3 forward = tank.transform.TransformDirection(Vector3.forward);
                Vector3 targetDir =tank.transform.position-target.position;//求出2者之间的距离
               
                if((Vector3.Angle(forward, targetDir) <10.0)||((Vector3.Angle(forward, targetDir) >-10.0))) //这边是自动开炮的效果
                {
                        cun_time++;
                        if(cun_time==260)
                        {
                                fire ();
                                cun_time=0;
                        }
                }
               
                if((Vector3.Distance(transform.position, this.transform.position) < 50))//发现目标而且在范围之内,开始追击目标
                {                       
                        Vector3 relativePos2 = target.position - tank.transform.position;
                        Quaternion targetRotation2 = Quaternion.LookRotation (relativePos2);
                       
                        tank.transform.rotation = Quaternion.Slerp(tank.transform.rotation, targetRotation2, Time.deltaTime * 1.0f);
                        tank.transform.Translate(Vector3.forward*0.05f);
                }
        }
       
        void fire()
        {
                GameObject obj=GameObject.CreatePrimitive(PrimitiveType.Sphere);
                obj.transform.localScale=new Vector3(0.5f,0.5f,0.5f);
                obj.transform.position=this.transform.position;
                obj.transform.rotation=this.transform.rotation;
                obj.AddComponent<Rigidbody>();
                obj.AddComponent<Bullet>();
                Physics.IgnoreCollision(obj.collider, collider);                               
        }
}

效果好像差的蛮远,拜托各位大大指点指点
发表于 2017-3-26 11:18 | 显示全部楼层
顶顶多好
发表于 2017-3-26 11:28 | 显示全部楼层
说的非常好
发表于 2017-3-26 11:55 | 显示全部楼层
很好哦
发表于 2017-3-26 11:54 | 显示全部楼层
不错不错
发表于 2017-3-26 11:15 | 显示全部楼层
LZ真是人才
发表于 2017-4-12 08:07 | 显示全部楼层
很不错
发表于 2017-4-12 08:35 | 显示全部楼层
顶顶多好
发表于 2017-4-12 08:55 | 显示全部楼层
很好哦
发表于 2017-4-12 08:30 | 显示全部楼层
不错不错
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-4-30 20:12 , Processed in 1.136941 second(s), 27 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表