一、初始化游戏环境
1.新建一个Plane,Scale的x= 2 ,z=2;
2.新建一个Sphere,position的y=0.5
3.创建材质文件夹,做一个蓝色的材质
4.在小球上面添加一个刚体组件“”
二、通过键盘按键控制小球的移动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Main : MonoBehaviour
{
private Rigidbody rd;
public float speed = 5;
// Start is called before the first frame update
void Start()
{
//1
//GameObject y = GameObject.FindWithTag("play");//获取游戏物体
//Rigidbody c = y.GetComponent<Rigidbody>();//获取刚体组件
//2
//Rigidbody y = GetComponent<Rigidbody>();//获取当前游戏物体的刚体组件
//3
//y = shopt.GetComponent<Rigidbody>();//获取拖拽游戏物体的刚体组件
rd = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
rd.AddForce(new Vector3(h,0,v)*speed);//移动 = 力的大小、力的方向、
}
}
三、控制相机的跟随
1.新建一个脚本,定义一个playerTransform,把主角(圆)拖过去
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Play_camera : MonoBehaviour
{
public Transform playerTransform;
public Vector3 offset;
// Start is called before the first frame update
void Start()
{
offset = transform.position - playerTransform.position;//当前的位置 - 主角的位置=求出一个偏移值
}
// Update is called once per frame
void Update()
{
transform.position = playerTransform.position + offset; //相机的位置 = 主角的位置+偏移值
}
}
四、创建墙和可收集的食物
1.新建一个cube,通过Scale调整cube的大小,做4个墙通过position和Rotation,调整位置。新建一个空的游戏对象Ground,拖进去,墙完成
2.新建一个cube,调整Rotation,为x = 45,y=45,z=45;做一个旋转的小食物,新建“Prefabs”预制体存放文件夹,把cube拖进去,再通过左上角的“Global”世界坐标去进行,复制粘贴,完成位置的摆放,最后新建一个游戏物体pickups拖进去。
五、控制食物的旋转
1.新建一个旋转的代码托给预制体中的小食物。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Retorecube : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Rotate(new Vector3(0, 1, 0));//围绕自身Y轴旋转
}
}
六、碰撞检测
1.添加代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Main : MonoBehaviour
{
private Rigidbody rd;
public float speed = 5;
// Start is called before the first frame update
void Start()
{
rd = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
rd.AddForce(new Vector3(h,0,v)*speed);//移动 = 力的大小、力的方向、
}
private void OnCollisionEnter(Collision collision)
{
//collision.collider获取碰撞到的游戏物体上的Collider组件
//string name = collision.collider.name;//获取碰撞到的游戏物体上的Collider组件的名字
if (collision.collider.tag == "pickup") {//获取碰撞的tag名称
Destroy(collision.collider.gameObject);//销毁游戏物体
}
}
}
七、捡起食物进行优化(碰撞检测不行,要用到触发检测)
1.在预制体cube面板中开启Is Trigger属性,打上对勾
2.编写触发检测的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Main : MonoBehaviour
{
private Rigidbody rd;
public float speed = 5;
// Start is called before the first frame update
void Start()
{
rd = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
rd.AddForce(new Vector3(h,0,v)*speed);//移动 = 力的大小、力的方向、
}
//碰撞检测(有物理效果阻挡)//2种检测,第一种可以不写
private void OnCollisionEnter(Collision collision)
{
//collision.collider获取碰撞到的游戏物体上的Collider组件
//string name = collision.collider.name;//获取碰撞到的游戏物体上的Collider组件的名字
if (collision.collider.tag == "pickup") {//获取碰撞的tag名称
Destroy(collision.collider.gameObject);//销毁游戏物体
}
}
//触发检测(没有物理效果的阻挡)
private void OnTriggerEnter(Collider other)
{
if (other.tag == "pickup")
{//获取碰撞的tag名称
Destroy(other.gameObject);//销毁游戏物体
}
}
}
八、显示分数和胜利检测
1.新建一个text,在左上角,点开,然后按住Alt按住不放,点选如图2所示,进行一个文字的左上角对齐。
2.在代码中编写代码,定义一个公有的对象,把Text拖拽给它
3.胜利检测,我们想要吃掉所有食物后,进行一个提示,需要在text下面,重新创一个text1,并调整它的宽高,因为字体的显示跟宽高有联系,所以必须调整,其次要将text1隐藏掉,点击text1名称旁边的对勾就可以隐藏
3.代码更新
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Main : MonoBehaviour
{
private Rigidbody rd;
public float speed = 5;
public Text text;
public GameObject textA;
int Score = 0;
// Start is called before the first frame update
void Start()
{
rd = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
rd.AddForce(new Vector3(h,0,v)*speed);//移动 = 力的大小、力的方向、
}
//碰撞检测(有物理效果阻挡)//2种检测,第一种可以不写
private void OnCollisionEnter(Collision collision)
{
//collision.collider获取碰撞到的游戏物体上的Collider组件
//string name = collision.collider.name;//获取碰撞到的游戏物体上的Collider组件的名字
if (collision.collider.tag == "pickup") {//获取碰撞的tag名称
Destroy(collision.collider.gameObject);//销毁游戏物体
}
}
//触发检测(没有物理效果的阻挡)
private void OnTriggerEnter(Collider other)
{
if (other.tag == "pickup")
{//获取碰撞的tag名称
Destroy(other.gameObject);//销毁游戏物体
Score++;
text.text = Score.ToString();
if (Score > 12) {
textA.SetActive(true);//SetActive此方法只对,类型为游戏物体有效
}
}
}
}
九、打包完成游戏