赞
踩
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.将编写的脚本挂载到相应的位置
这样,我们就可以通过单击鼠标左键,来发射出子弹了!!!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。