Unity导弹,射击

导弹

(由于本鼠鼠是手上没有合适的素材又是美工苦手,就凑合着看吧...)

 红色即为导弹,蓝色即为目标

 在导弹上挂载钢体组件和碰撞组件

 ps:勾选碰撞组件中的is Trigger属性

 在目标上挂载碰撞组件

 创建C#脚本

代码实现 

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

[RequireComponent(typeof(Rigidbody2D))]
public class Missile : MonoBehaviour
{
    public Transform target;

    public float speed=5f;//导弹的飞行速度
    public float rotateSpeed=200f;//导弹的转弯速度

    private Rigidbody2D rigidbody2D;

    private void Start() {
        rigidbody2D=GetComponent<Rigidbody2D>();
    }

    private void FixedUpdate() {
        //目标相对于导弹的方向向量
        Vector2 direction = (Vector2)target.position-rigidbody2D.position;
        
        direction.Normalize();
        
        float rotateAmount=Vector3.Cross(direction,transform.up).z;
        
        //导弹的角速度
        rigidbody2D.angularVelocity=-rotateAmount*rotateSpeed;

        //导弹的线速度
        rigidbody2D.velocity=transform.up*speed;
    }
    
    //导弹命中
    private void OnTriggerEnter2D(Collider2D target) {
        Destroy(target.gameObject);
        Destroy(gameObject);
    }
}

对于导弹来说,当目标不在其左侧就在其右侧 

 当所需的旋转的角度越大,叉乘得到的结果向量模长越长(可以通过叉乘所得到向量的Z轴)

 Rigidbody2D.angularVelocity:角速度(以度/秒为单位)

rigidbody2D.angularVelocity=-rotateAmount*rotateSpeed;

ps:一定要加负号,否则导弹会被“排斥”,而非跟踪

如图

射击

创建一个炮台

 如图所示的双管炮台,蓝色的炮台包括两个子物体,即左右炮管。左右炮管分别包括了一个空物体firePos,用于确定子弹被实例化的位置

创建子弹

为子弹挂载钢体组件和碰撞组件

 靶子就沿用上图中的target,但是为其创建一个名为target的新Tag

创建C#脚本,Bullet,Turret,Weapon并挂载至子弹,炮台,两个炮管

代码实现

Weapon

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

public class Weapon : MonoBehaviour
{
    public Transform firePos;//设置开火位置
    public GameObject bulletPrefab;//发射的子弹

    private void Update() {
        //鼠标左键攻击
        if(Input.GetMouseButtonDown(0)){
            Shoot();
        }
    }

    private void Shoot(){
        //实例化子弹
        Instantiate(bulletPrefab,firePos.position,firePos.rotation);
    }
}

Bullet

public class Bullet : MonoBehaviour
{
    public float speed = 20f;//子弹的速度
    public Rigidbody2D rb;

    void Start()
    {
        rb.velocity = transform.up*speed;
    }

    private void OnTriggerEnter2D(Collider2D target) {
        //命中后摧毁靶子
        if(target.tag=="target"){
            Destroy(target.gameObject);
        }
        Destroy(gameObject);
    }
}

Turret

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

public class Turret : MonoBehaviour
{

    void Update() 
    {      
        //鼠标在世界坐标的位置
        Vector2 mousePos=Camera.main.ScreenToWorldPoint(Input.mousePosition);
        //炮台要指向的方向
        Vector2 aimPos=mousePos-(Vector2)transform.position.normalized;
        //将物体自身坐标系的x轴朝向aimPos
        transform.up=aimPos;
    }
}

附赠一个简单的激光枪 

 ​​​​​创建一个LineRenderer

记得调整LineRenderer中Position的值

 创建相应的武器

 代码实现

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

public class LaserWeapon : MonoBehaviour
{
    public Transform firePos;
    public LineRenderer laser;

    private void Update() {
        if(Input.GetMouseButtonDown(0)){
            //启动协程
            StartCoroutine(Shoot()) ;
        }
    }

    IEnumerator Shoot(){
        //射线检测
        RaycastHit2D hit=Physics2D.Raycast(firePos.position,firePos.right,100);
        if(hit){
            //射程内有物体(距离<=100)
            laser.SetPosition(0,firePos.position);
            laser.SetPosition(1,hit.point);
            Destroy(hit.collider.gameObject);
        }else{
            //射程内没有物体(距离>100)
            laser.SetPosition(0,firePos.position);
            laser.SetPosition(1,firePos.position+firePos.right*100);
        }
        laser.enabled=true;
        //激光停留的时间
        yield return new WaitForSeconds(0.02f);
        laser.enabled=false;
    }
    
}

借用上面写好的组件,我们可以完成一个小游戏

玩法是地图上随机生成导弹,玩家要躲避来袭的导弹,同时可以击毁导弹

在背景上挂载名为Missile_Dodge的C#脚本

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

public class Missile_Dodge : MonoBehaviour
{
    public GameObject Missile;
    public Transform pos;
    private float timer=3;//生成时间间隔

    Vector2 random_pos;

    void Update() {
        //每隔3s在随机位置生成导弹
        if(timer>=3){
            Generator();
            timer=0;
        }else{
            timer+=Time.deltaTime;
        }
        
    }

    void Generator(){
        random_pos.x=Random.Range(-10,10.1f);
        random_pos.y=Random.Range(-4.5f,4.6f);
        pos.position=random_pos;
        Instantiate(Missile,pos.position,pos.rotation);
    }
}

玩家控制脚本 

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

public class MouseFlying : MonoBehaviour
{
    private float moveSpeed=5f;

    void Update()
    {
        transform.Translate(transform.up*moveSpeed*Time.deltaTime,Space.World);
        //转向鼠标的位置
        Vector2 dir=Camera.main.ScreenToWorldPoint(Input.mousePosition)-transform.position;
        transform.up=dir;
    }
}

效果图

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值