|
楼主 |
发表于 2025-1-7 12:11:33
|
显示全部楼层
子弹脚本
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Bullet : MonoBehaviour
- {
- public float maxRight;
- public float speed = 10f;
- // Start is called before the first frame update
- void Start()
- {
- maxRight = GameObject.Find("right").transform.position.x;
- }
- // Update is called once per frame
- void Update()
- {
- //子弹飞行
- transform.Translate(Vector2.right * Time.deltaTime * speed);
- //自动销毁
- if (this.transform.position.x> maxRight)
- {
- DestroyImmediate(this.gameObject);
- }
- }
- }
复制代码
Player01脚本
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- public class PlayerController : MonoBehaviour
- {
- public float fireRage=0.5f;
- public float nextFire=0f;
- public float speed = 1f;
- public GameObject bullet;
- private Transform shootingPos;
- public float maxTop;
- public float minDown;
- // Start is called before the first frame update
- void Start()
- {
- shootingPos = GameObject.Find("ShootingPos").transform;
- }
- // Update is called once per frame
- void Update()
- {
- print(Time.time);
- //玩家移动脚本
- float v=Input.GetAxisRaw("Vertical");
- if (v != 0)
- {
- transform.Translate(Vector2.up * v * Time.deltaTime * speed);//(new Vector2(0,1))
- }
- //发射子弹
- if (Input.GetKeyDown(KeyCode.U)&& Time.time> nextFire)
- {
- Instantiate(bullet, shootingPos.position, Quaternion.identity);
- nextFire = Time.time+fireRage;
- }
- //控制玩家不要出边界
- //上边界
- if (this.transform.position.y>maxTop)
- {
- this.transform.position = new Vector3(this.transform.position.x, maxTop,this.transform.position.z);
- }
- //下边界
- if (this.transform.position.y < minDown)
- {
- this.transform.position = new Vector3(this.transform.position.x, minDown, this.transform.position.z);
- }
- }
- }
复制代码 |
|