打砖块实现效果
准备开始学unity,跟着视频做了一个简单的小游戏,因为真的什么都不懂,花了很久的时间,做完回头看看,真的是很菜啦/(ㄒoㄒ)/~~
1.场景
Env中包含很多cube,用来做砖块,plane是地平面
(这里不需要小球,小球作为prefab,在鼠标点击时实例化)
2.project
materials是材质,用来给cube等上色
3.scripts
(1)shoot类用来发射小球,作为相机的组件,因为从相机处发射小球
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class shoot : MonoBehaviour {
// Use this for initialization
public GameObject bullet;
public float speed=5;
void Start () {
Debug.Log("Hello Unity!");
// GameObject.Instantiate(bullet,transform.position,transform.rotation);
}
// 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;
//给刚体一个初速度,用来发射小球
}
}
}
(2)movement类用来移动相机,也作为相机的组件,使得小球能从不同的地方发射
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movement : MonoBehaviour {
public float speed = 3;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float h = Input.GetAxis("Horizontal"); //获取键盘左右键控制移动的距离
float v = Input.GetAxis("Vertical"); //获取键盘上下键控制移动的距离
Debug.Log(h);
transform.Translate(new Vector3(h, v, 0) * Time.deltaTime*speed); //移动相机
}
}
注意:1.prefab小球须添加刚体组件
2.制作砖块墙时,可用一个砖块ctrl+D 复制,再按住ctrl向旁边拖拽,就可形成两个紧贴的砖块。