当前位置:   article > 正文

Unity 3D子弹发射制作

Unity 3D子弹发射制作

1.导入一个枪的模型,调整好枪的位置

2.点击ScifiRifle(枪)右键创建一个Crete Empty(空的物体)命名fireponint代表子弹生成的位置

3. 制作一个简易的子弹,右键3D object→spher这里我们选用spher将其命名为bulletpoint放到合适的位置

4.编写子弹发射脚本 点击Add Component→Guncontral在Assets右键Create→C# Script

 

5脚本内容 

 

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

public class guncontral : MonoBehaviour
{
    //子弹生成位置
    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(bulletper, bulletpoint.position, bulletpoint.rotation);
            bulletCount--;
            
        }
        if(bulletCount == 0)
        {
            GetComponent<Animator>().SetTrigger("Reload");
            Reload();
        }
        if (Input.GetKeyDown(KeyCode.R) && bulletCount != 10)
        {
            GetComponent<Animator>().SetTrigger("Reload");
            Reload();
        }
    }
  
}
6.将编写的脚本挂载到相应的位置

 这样,我们就可以通过单击鼠标左键,来发射出子弹了!!!

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/597176
推荐阅读
相关标签
  

闽ICP备14008679号