Unity3D学习从简单例子入门,后续将持续更新。
一、跑道建立
可通过材质球Rendering Mode : Transparent 和 Albedo 中A值来控制透明度
也可将已有的材质拖拽至Assets中,方便拖拽直接达到复制效果(Ctrl+D);
二、创建管理员
create empty 创建管理员对象,用于绑定管理员脚本,定义游戏结束、重新开始、过关方法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;//引入Unity 界面 SampleScene 管理
using UnityEngine.UI;//对界面操作 引入UI
public class GM : MonoBehaviour
{
//物体对应声明 可在Untiy界面 GM 脚本 属性中配置指定Panel对象实体化
public GameObject panel;
// Start is called before the first frame update
void Start()
{
//panel物体隐藏
panel.SetActive(false);
}
// Update is called once per frame
void Update()
{
}
//自定义游戏结束方法
public void GameOver() {
//让游戏停下来 游戏世界时间停止
Time.timeScale = 0;
//panel物体显示
panel.SetActive(true);
//修改界面提示文字
Text t = panel.transform.Find("Text").GetComponent<Text>();
t.text = "Game Over!";
}
//自定义游戏重新开始方法
public void GameRestart() {
//游戏世界时间重新开始
Time.timeScale = 1;
//重新开始
SceneManager.LoadScene(0);
}
//游戏过关
public void GamePass() {
//让游戏停下来 游戏世界时间停止
Time.timeScale = 0;
//panel物体显示
panel.SetActive(true);
//修改界面提示文字
Text t = panel.transform.Find("Text").GetComponent<Text>();
t.text = "通关!";
}
}
三、障碍物设定
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Box : MonoBehaviour
{
//当小球撞到障碍物
private void OnCollisionEnter(Collision collision)
{
//让游戏停下来 游戏世界时间停止
//Time.timeScale = 0;
//使用GameObject.Find方法获取Unity界面SampleScene中已创建对象GM中属性GM脚本类中的GameOver方法
GameObject.Find("GM").GetComponent<GM>().GameOver();
}
}
四、小球
设定刚体
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour
{
Rigidbody r;//声明刚体对象 刚体可以模拟现实中物体的运动,具有速度
public float speed = 10;//速度 默认10
float p = 1.5f;
float l = 0;
// Start is called before the first frame update
void Start()
{
r = GetComponent<Rigidbody>();//获取刚体对象组件
}
// Update is called once per frame
void Update()
{
float x = Input.GetAxis("Horizontal");//获取方向键左右输入值
float z = Input.GetAxis("Vertical");//获取方向键上下
if (z > 0)
{
speed = speed + 3;
}
else if (z < 0)
{
speed = speed <= 0? 0:(speed - 3);
}
else {
speed = 6;
}
r.velocity = new Vector3(x*8,r.velocity.y,speed);//速度 x,y,z
if (Time.time >= l+p) {
Vector3 f = new Vector3(0,5,0);
r.AddForce(f,ForceMode.Impulse);//给小球施加力f
l = Time.time;
}
}
}
五、障碍物
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Finish : MonoBehaviour
{
//当小球进入终点体内时
private void OnTriggerEnter(Collider other)
{
//使用GameObject.Find方法获取Unity界面SampleScene中已创建对象GM中属性GM脚本类中的GamePass方法
GameObject.Find("GM").GetComponent<GM>().GamePass();
}
}
六、小球绑定视角跟随
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//视角跟着小球走 例如放风筝
public class Follow : MonoBehaviour
{
public Transform t;//声明 定义一个人 在unity 界面 Follow 脚本属性中对t对象进行实体化
Vector3 o;//定义了一条风筝线
// Start is called before the first frame update
void Start()
{
//在游戏刚开始的时候确定好风筝线的长度、角度等信息
o = transform.position - t.position;
}
// Update is called once per frame
void Update()
{
//让每时每刻风筝都被人和线拖着走
transform.position = t.position + o;
float h = Input.GetAxis("Horizontal");
}
}