C#小游戏之打砖块
打磚塊
小游戲可以分為兩步:
1.控制移動
2.控制射擊
具體代碼如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 控制左右移動
/// </summary>
public class Movement : MonoBehaviour {
? ? public float speed = 3f;
?? ?// Update is called once per frame
?? ?void Update () {
? ? ? ? float h = Input.GetAxis("Horizontal");
? ? ? ? float v = Input.GetAxis("Vertical");
? ? ? ? transform.Translate(new Vector3(h, v, 0) * Time.deltaTime * speed );
?? ?}
}
using UnityEngine;
/// <summary>
/// 發射子彈
/// </summary>
public class Shoot : MonoBehaviour {
? ? public GameObject bullet;
? ? public float speed = 5;
?? ?// Update is called once per frame
?? ?void Update () {
? ? ? ? if ( Input.GetMouseButtonDown(0) )
? ? ? ? {
? ? ? ? ? ? GameObject b = GameObject.Instantiate(bullet, transform.position, transform.rotation);
? ? ? ? ? ? Rigidbody rgd = b.GetComponent<Rigidbody>();
? ? ? ? ? ? rgd.velocity = transform.forward * speed;
? ? ? ? }
? ? }
}
注意:
子彈上必須有剛體,碰撞盒,且磚塊上要有碰撞盒,這樣才能產生碰撞效果
總結
- 上一篇: matlab对于椭圆检测的算法,基于弧段
- 下一篇: 统计建模与R软件 第三章课后习题 不包含