制作过程:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
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;
// Start is called before the first frame update
void Start()
{
}
// 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--;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class bullet : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
GetComponent<Rigidbody>().AddForce(transform.forward * 2000);
}
// Update is called once per frame
void Update()
{
}
private void OnCollisionEnter(Collision collision)
{
Destroy(gameObject);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Move : MonoBehaviour
{
public Text fenshu;
public int score = 0;
public GameObject shengli;
private Rigidbody rg;
// Start is called before the first frame update
void Start()
{
rg = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
rg.AddForce(new Vector3(h,0,v));
}
//private void OnCollisionEnter(Collision collision)//碰撞方法
//{
// if (collision.gameObject.tag == "food")
// {
// Destroy(collision.gameObject);
// }
//}
private void OnTriggerEnter(Collider other)//触发器的方法
{
if (other.gameObject.tag == "food")
{
Destroy(other.gameObject);
score++;
fenshu.text = "分数:"+score ;//字符串拼接
if (score==12) {
shengli.SetActive(true);
}
}
}
//private void OnCollisionStay(Collision collision)
//{
// Debug.Log("持续发生");
//}
//private void OnCollisionExit(Collision collision)
//{
// Debug.Log("结束碰撞");
//}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
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;
// Start is called before the first frame update
void Start()
{
}
// 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--;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class bullet : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
GetComponent<Rigidbody>().AddForce(transform.forward * 2000);
}
// Update is called once per frame
void Update()
{
}
private void OnCollisionEnter(Collision collision)
{
Destroy(gameObject);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Move : MonoBehaviour
{
public Text fenshu;
public int score = 0;
public GameObject shengli;
private Rigidbody rg;
// Start is called before the first frame update
void Start()
{
rg = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
rg.AddForce(new Vector3(h,0,v));
}
//private void OnCollisionEnter(Collision collision)//碰撞方法
//{
// if (collision.gameObject.tag == "food")
// {
// Destroy(collision.gameObject);
// }
//}
private void OnTriggerEnter(Collider other)//触发器的方法
{
if (other.gameObject.tag == "food")
{
Destroy(other.gameObject);
score++;
fenshu.text = "分数:"+score ;//字符串拼接
if (score==12) {
shengli.SetActive(true);
}
}
}
//private void OnCollisionStay(Collision collision)
//{
// Debug.Log("持续发生");
//}
//private void OnCollisionExit(Collision collision)
//{
// Debug.Log("结束碰撞");
//}
}