打砖块
小游戏可以分为两步:
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;
}
}
}
注意:
子弹上必须有刚体,碰撞盒,且砖块上要有碰撞盒,这样才能产生碰撞效果