unity|敌方坦克自由移动,当靠近到一定距离后,可自动攻击

1、场景里新建四个东西
在这里插入图片描述
新建Empty:Hierarchy面板下右击->Create Empty
2、Empty的位置是在diren的炮口处
在这里插入图片描述
3、制作子弹的预制体
在这里插入图片描述
在这里插入图片描述
4、diren挂上此代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class chatAI : MonoBehaviour {

  
    public GameObject Player;
    public float movementSpeed = 4;
    public GameObject bullet;
    public Transform bullstSpawn;
    float time = 0;
    void Start()
    {
        //_Agent = this.GetComponent<UnityEngine.AI.NavMeshAgent>();
    }

    // Update is called once per frame
    void Update()
    {
        float x = Random.Range(0, 20);
        float z = Random.Range(-10, 10);

        while (x > 0)
        {
            this.transform.position += this.transform.forward * x * Time.deltaTime;
            this.transform.eulerAngles += new Vector3(0.0f, 30.0f, 0.0f) * z * Time.deltaTime;
            x -= 10;
        }
        movementSpeed = 1;//坦克速度
        
            
        float dist = Vector3.Distance(Player.transform.position, transform.position);//坦克与小车之间的距离
        if (dist < 20)
        {
        transform.LookAt(Player.transform);//坦克朝向小车
            movementSpeed = 2;//车后的速度变4
                
            time += Time.deltaTime;//计时
            if (time > 2)//当坦克与车的距离小于20大于2s后
            {
                Fire();//开火
                    
                time = 0;//时间重置
            }
               
        }
      
        transform.position += transform.forward * movementSpeed * Time.deltaTime;//改变坦克位置

    }
    void Fire() {//开火
        GameObject bulletgo = Instantiate(bullet,bullstSpawn.position, bullstSpawn.rotation);//实例化子弹
        bulletgo.GetComponent<Rigidbody>().velocity = transform.forward * 20.0f;//子弹运动
        Destroy(bulletgo, 2);//两秒后消灭子弹
    }
}

Player----car(Hierarchy)
Bullst Spawn-----e(Hierarchy)
Bullet----bullet(Project)
在这里插入图片描述

5、car挂上此代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class carmove : MonoBehaviour {

	// Use this for initialization
	void Start () {
        //GetComponent<Rigidbody>().freezeRotation = true;
    }
    // Update is called once per frame
    void Update()
    {
        
        float y = Input.GetAxis("Horizontal") * Time.deltaTime * 150.0f;//旋转角度,水平方向上的变换
        float z = Input.GetAxis("Vertical") * Time.deltaTime * 10.0f;//控制前后

        transform.Rotate(0, y, 0);//绕y轴旋转

        transform.Translate(0, 0, z);//在水平方向上运动位置

    }
    private void OnCollisionEnter(Collision coll) {//坦克的碰撞检测
        if (coll.transform.tag == "diren") {//if (coll.transform.name == "diren")
            
        }
    }
}

效果
在这里插入图片描述

  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值