unity如何创建枪械射击

目录

目录

目录

1.原理  通过代码生成物体并从枪械射出

展示画面 

2.实现过程

 1.创建枪械

2.创建小球 

3.创建UI 

3.代码导入

4.总结

1.完整代码:



1.原理  通过代码生成物体并从枪械射出

展示画面 

2.实现过程

 1.创建枪械

将准备好的枪械模型导入并移动至摄像机的列表下

2.创建小球 

在左栏右键-3D Obect-Sphere创建小球

将小球设置到合适大小后拖入资源栏并删除本体

3.创建UI 

左栏右键-UI-Image添加UI图片

按住Alt点击居中

枪械准备动作基本完成

3.代码导入

 给枪械创建代码

在最上方添加一栏

using UnityEngine.UI;

 以控制UI

 创建以下代码

 //枪口火焰生成位置
    public Transform firepoint;
    //枪口火焰物体
    public GameObject fireper;
    //子弹生成位置
    public Transform bulletpoint;
    //子弹物体
    public GameObject bulletper;
    //子弹个数
    private int bulletCount = 10;
    // 开火间隔
    private float cd = 0.2f;
    //实际开火的时间 计时器
    private float timer = 0;
    private AudioSource gunvoice;
    public AudioClip clip;
    public Text bulletcount;

开火间隔与个数可自行根据调节

 //创建生成位置
    public Transform firepoint;
    //创建物体
    public GameObject fireper;

创建后会在代码区域出现创建物体选项

 

开火代码为

    void Start()
    {
        gunvoice = GetComponent<AudioSource>();
    }

    // Update is called once per frame
    void Update()
    {
        //计算实际开火间隔
        timer = timer + Time.deltaTime;
        if (Input.GetMouseButton(0) && bulletCount > 0 && timer > cd)
        {//开枪

            timer = 0;
            //火焰生成
            Instantiate(fireper, firepoint.position, firepoint.rotation);
            //子弹生成
            Instantiate(bulletper, bulletpoint.position, bulletpoint.rotation);
            bulletCount--;//bulletcount=bulletcount-1
            gunvoice.PlayOneShot(clip);
            bulletcount.text = "子弹数:" + bulletCount;//  100 "100"
        }
        if (bulletCount == 0)
        {

            //Invoke("Reload", 2.0f);
            GetComponent<Animator>().SetTrigger("Reload");
            Reload();
        }
        if (Input.GetKeyDown(KeyCode.R) && bulletCount != 10)
        {
            GetComponent<Animator>().SetTrigger("Reload");
            Reload();
        }
    }

 Ctrl+s保存代码后返回unity

将准备好的开火动画和音频拖入代码栏

至此,即可实现左键开火射击

4.总结

通过代码即可实现开火,资源物体可以自己挑选。

1.完整代码:

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

public class guncontral : MonoBehaviour
{
    //枪口火焰生成位置
    public Transform firepoint;
    //枪口火焰物体
    public GameObject fireper;
    //子弹生成位置
    public Transform bulletpoint;
    //子弹物体
    public GameObject bulletper;
    //子弹个数
    private int bulletCount = 10;
    // 开火间隔
    private float cd = 0.2f;
    //实际开火的时间 计时器
    private float timer = 0;
    private AudioSource gunvoice;
    public AudioClip clip;
    public Text bulletcount;

    // Start is called before the first frame update
    void Start()
    {
        gunvoice = GetComponent<AudioSource>();
    }

    // Update is called once per frame
    void Update()
    {
        //计算实际开火间隔
        timer = timer + Time.deltaTime;
        if (Input.GetMouseButton(0) && bulletCount > 0 && timer > cd)
        {//开枪

            timer = 0;
            //火焰生成
            Instantiate(fireper, firepoint.position, firepoint.rotation);
            //子弹生成
            Instantiate(bulletper, bulletpoint.position, bulletpoint.rotation);
            bulletCount--;//bulletcount=bulletcount-1
            gunvoice.PlayOneShot(clip);
            bulletcount.text = "子弹数:" + bulletCount;//  100 "100"
        }
        if (bulletCount == 0)
        {

            //Invoke("Reload", 2.0f);
            GetComponent<Animator>().SetTrigger("Reload");
            Reload();
        }
        if (Input.GetKeyDown(KeyCode.R) && bulletCount != 10)
        {
            GetComponent<Animator>().SetTrigger("Reload");
            Reload();
        }
    }

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity大作业射击类是一种基于Unity游戏引擎开发的射击游戏。在这个项目中,我们可以创建一个全新的游戏场景,包括各种射击元素和关卡设计。 首先,我们可以设计多个射击角色,如玩家角色和敌人角色。玩家可以选择不同的武器来与敌人战斗。我们可以使用Unity提供的游戏对象来创建角色模型和动画,同时利用物理引擎来实现射击、击中和碰撞效果。 其次,我们可以设计不同的游戏关卡。每个关卡可以有不同的地图、敌人布置和目标任务。例如,玩家需要在限定的时间内击败所有敌人,或者必须保护一个重要目标不被敌人攻击。我们可以使用Unity的场景编辑器来创建关卡地图,并通过脚本来控制敌人的行为和AI。 还可以添加一些特殊元素来增加游戏的趣味性和挑战性。例如,可以在地图中放置道具,玩家可以拾取并使用这些道具来增强自己的能力。另外,可以设置障碍物、陷阱和隐藏的秘密路径,增加关卡的变化和思考性。 最后,我们还可以在游戏中添加音效和背景音乐来增强游戏的氛围。可以在玩家射击、敌人死亡和任务完成等关键事件中增加音效,让玩家更加投入到游戏中。 总之,Unity大作业射击类是一个非常有趣而有挑战性的项目。通过利用Unity引擎的强大功能,我们可以实现一个精彩的射击游戏,给玩家带来丰富的游戏体验。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值