目标:
using System.Collections;using System.Collections.Generic;using UnityEngine;public class Tank : MonoBehaviour{ public GameObject projPrefab; // 要发射的子弹的预制体 public Transform shotPos; // 要发射的子弹的位置 private float lastFireTime; // 上一次发射的时间 // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { if (Input.GetButton("Fire1") && Time.time - lastFireTime > 0.2f) { // 实例化(从预制体克隆)一个子弹 var transProj = Instantiate(projPrefab).transform; // 设置子弹的位置在坦克设定好的炮口位置 transProj.SetParent(this.transform, false); transProj.position = shotPos.position; Vector3 v = shotPos.forward * 100; transProj.GetComponent<Rigidbody>().AddForce(v, ForceMode.Impulse); Destroy(transProj.gameObject, 2f); lastFireTime = Time.time; } }}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tank : MonoBehaviour
{
public GameObject projPrefab; // 要发射的子弹的预制体
public Transform shotPos; // 要发射的子弹的位置
private float lastFireTime; // 上一次发射的时间
// Start is called before the first frame update
void Start()
}
// Update is called once per frame
void Update()
if (Input.GetButton("Fire1")
&& Time.time - lastFireTime > 0.2f)
// 实例化(从预制体克隆)一个子弹
var transProj = Instantiate(projPrefab).transform;
// 设置子弹的位置在坦克设定好的炮口位置
transProj.SetParent(this.transform, false);
transProj.position = shotPos.position;
Vector3 v = shotPos.forward * 100;
transProj.GetComponent<Rigidbody>().AddForce(v, ForceMode.Impulse);
Destroy(transProj.gameObject, 2f);
lastFireTime = Time.time;
原文链接:http://www.cnblogs.com/raymondking123/p/11445369.html
本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728