飞机脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyJet : MonoBehaviour
{
public GameObject myprefab;
// Start is called before the first frame update
void Start()
{
Application.targetFrameRate = 60; //固定帧率
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0)) {
Fire(); // 开火
}
}
private void Fire() {
GameObject bullet = Instantiate(myprefab);
bullet.transform.position = transform.position + new Vector3(0, 1f, 0);
}
}
?预制体子弹脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyBullet : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
float step = 1.6f * Time.deltaTime; //每帧移动的距离
transform.Translate(0, step, 0, Space.Self);
Vector3 sp = Camera.main.WorldToScreenPoint(transform.position);
if (sp.y > Screen.height) {
Destroy(this.gameObject); //销毁子弹
}
}
}
|