Unity制作一把可以发射子弹的枪 您所在的位置:网站首页 怎样用纸做一把枪可以发射子弹 Unity制作一把可以发射子弹的枪

Unity制作一把可以发射子弹的枪

2024-06-17 06:41| 来源: 网络整理| 查看: 265

1.制作一个简易人物(player)与枪模型(gun),把摄像头和枪拖到人物模型下让他们成为人物的子对象。然后给摄像机加一个可以用鼠标控制的代码(如下)这样就可以控制视角与枪了。

using System.Collections; using System.Collections.Generic; using UnityEngine; public class Dyuan : MonoBehaviour { private Transform head; private Transform body; // Start is called before the first frame update void Start() { head = transform; body = transform.parent; Cursor.lockState = CursorLockMode.Locked; } // Update is called once per frame void Update() { float mousex = Input.GetAxis("Mouse X"); if (mousex != 0) { body.Rotate(Vector3.up, mousex * 200 * Time.deltaTime); } float mousey = Input.GetAxis("Mouse Y"); if (mousey != 0) { head.Rotate(Vector3.left, mousey * 200 * Time.deltaTime); } if (Vector3.Angle(body.forward, head.forward) > 60) { head.Rotate(Vector3.left, mousey * -100 * Time.deltaTime); } if (Input.GetKey(KeyCode.W)) { body.Translate(Vector3.forward * 5.0f * Time.deltaTime); } if (Input.GetKey(KeyCode.S)) { body.Translate(Vector3.back * 5.0f * Time.deltaTime); } if (Input.GetKey(KeyCode.A)) { body.Translate(Vector3.left * 5.0f * Time.deltaTime); } if (Input.GetKey(KeyCode.D)) { body.Translate(Vector3.right * 5.0f * Time.deltaTime); } } }

2.制作一枚子弹。将Sphere拖拽至Project下的Asset下使其成为一个预制体,我们可以将它的名字改为Bullet,然后再删除Hierarchy面板下的子弹。记得给子弹添加刚体属性添加刚体属性后才会有力学特征才会动。

3..接下来制作枪口,要创建一个空对象,用来发射子弹,在Hierarchy面板下单机右键,选择Create Empty,将它重命名为Bullet,然后在Scene里面拖动它的位置,使其位于枪口上,然后再将Bullet拖拽至人物下,使其成为人物的一个子物体。

4.然后制作子弹的代码,给它一个发射速度。

using System.Collections; using System.Collections.Generic; using UnityEngine; public class bullet : MonoBehaviour { // Start is called before the first frame update void Start() { GetComponent().AddForce(transform.forward * 1800); } // Update is called once per frame void Update() { } private void OnCollisionEnter(Collision collision) { Destroy(gameObject); } }

5.用UI制作界面子弹数显示和准心

6.之后用代码给枪加上换弹的动作和开枪时的火花,持续开火,射速,子弹数,之类的属性。然后把代码给枪

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 = 30; // 开火间隔 private float cd = 0.07f; //实际开火的时间 计时器 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(); } // 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) { timer = -1.5f;//换弹时间 //Invoke("Reload", 2.0f); GetComponent().SetTrigger("Reload"); Reload(); } if (Input.GetKeyDown(KeyCode.R ) && bulletCount != 30) { timer = -1.5f; GetComponent().SetTrigger("Reload"); Reload(); } } void Reload() {//换弹 bulletCount = 30; bulletcount.text = "子弹数:" + bulletCount; } }

之后把对应的材料文件与代码绑定。

7.这样就可以射击了,



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有