Unity游戏:一跳到底游戏制作
教程目录:
一、Unity游戏:一跳到底——创建新项目、导入资源、设置场景
1.创建新项目
File -> New project -> 输入Project name -> Preate project
2.导入资源
链接:https://pan.baidu.com/s/17EIGiDtIUNpcPM2zB-XRUw
提取码:x8kk
复制这段内容后打开百度网盘手机App,操作更方便哦
3.设置场景
保存场景
File -> Save Scene / 直接Ctrl+S -> 输入文件名“Game” -> 保存
保存完之后在Assets文件夹可以看到刚刚保存的场景。
设置build target
File -> Build Settings
默认的build平台是PC, Mac & Linux Standalone,也可以选择其他平台,例如iOS、Android等等。
可以在Build Settings窗口中点击Player Setting设置更多细节。
Player Setting也可以在Edit -> Project Settings -> Player中打开。
将宽高比设置为600*900:
二、Unity游戏:一跳到底——制作游戏场景、制作地板预制体、创建小球
1、制作游戏场景
创建一个圆柱
y 的大小设置为16
2、制作地板预制体
1、导入的资源包中找到合适的圆柱形面板进行组合(不想做这一步可以用博主做好的预制体)
2、相应的每块圆柱形面板加入collider组件并勾选Trigger
3、每种看自己喜好给颜色材质并都做成预制体(建议颜色种类不要太多)
4、然后做成如下样子(每个面板留一个口子方便小球小落)
3D场景图
2D场景图
每一层搞到一个空物体中去
3、创建小球
创建小球并给小球赋予材质和重力组件(小球名字为Plyer)
三、Unity游戏:一跳到底——小球的弹跳、相机的跟随、圆柱的旋转
1、小球的弹跳
1、新建脚本名为Player
2、为黑色面板添加标签Plane
3、小球接触面板涂鸦预制体
4、代码编写
private Rigidbody rg;
//小球的运动速度
public float speed;
void Start()
{
rg = GetComponent<Rigidbody>();
jumpMusic = GetComponent<AudioSource>();
speed = 6.8f;(速度自己调)
}
private void OnTriggerEnter(Collider col)
{
//小球碰到面板
if (col.tag == "Plane")
{
//小球向上运动
rg.velocity = Vector3.up * speed;
// 涂鸦生产(产生的位置根据自己小球的大小设置)
GameObject color = Instantiate(colorPrefab,
new Vector3(transform.position.x, col.transform.position.y+0.5f,
transform.position.z) , colorPrefab.transform.rotation);
Destroy(color,0.2f);
}
}
2、相机的跟随(该代码加入相机属性栏中)
1、代码编写(脚本名为Cam)
public Transform ball;
private Vector3 dis;
// Start is called before the first frame update
void Start()
{
//获得小球与相机的偏移量
dis = ball.position - transform.position;
}
// Update is called once per frame
void Update()
{
if (Mathf.Abs(transform.position.y-ball.position.y)>1.5f)
{
//得到相机目标的位置
Vector3 target = ball.position - dis;
// 插值法使相机移动到目标位置
transform.position = Vector3.Lerp(transform.position, target, Time.deltaTime*6);
}
}
3、圆柱的旋转(该代码加入圆柱属性栏中)
1、代码编写(脚本名为XZ)
private float x;
public float speed;
public GameObject colorPrefab;
// Start is called before the first frame update
void Start()
{
// 旋转速度
speed = 60;
float y = Random.Range(-90, 90);
//初始位置随机绕Y轴旋转
transform.Rotate(new Vector3(0, y, 0));
}
// Update is called once per frame
void Update()
{
//通过鼠标左键控制旋转
if (Input.GetMouseButton(0))
{
// 通过虚拟轴获取鼠标水平方向的移动增量
x = Input.GetAxis("Mouse X");
//transform.localEulerAngles = new Vector3(0, -x*speed, 0);
// 通过Rotate结合鼠标左键的移动增量旋转
transform.Rotate(new Vector3(0, x * -1, 0) * speed * Time.deltaTime);
}
}
四、Unity游戏:一跳到底——得分检测、失败检测
1、得分检测
1、创建空物体命名为并给它加Box Collider组件还要添加标签Score
2、检测范围调到自己适合大的大小有四个
3、得分UI显示文本命名为ScoreText并且文字大小和位子自己调
4、代码编写(在Plyer脚本编写)
using UnityEngine.UI;// 引入命名空间
// 得分UI
public Text scoreText;
public int score;
private void OnTriggerEnter(Collider col)
{
//小球得分检测
if (col.tag == "Score")
{
SetScore();
}
}
//得分方法
public void SetScore()
{
score++;
if (scoreText != null)
scoreText.text = score.ToString();
}
2、失败检测
1、给黄色面板预添加标签Error
2、失败按钮UI制作退出和重开按钮
3、给按钮添加背景图标
4、代码编写
using UnityEngine.UI;
public Button start;
public Button exit;
void Start()
{
// 游戏开始按钮不显示
start.gameObject.SetActive(false);
exit.gameObject.SetActive(false);
}
private void OnTriggerEnter(Collider col)
{
//小球游戏失败检测
if (col.tag == "Error")
{
// 按钮显示
start.gameObject.SetActive(true);
exit.gameObject.SetActive(true);
transform.position = new Vector3(transform.position.x,
transform.position.y + 0.2f, transform.position.z);
//游戏暂停
Time.timeScale = 0;
}
5、将相应的文本或者按钮和预制体拖入小球代码组件里去
五、Unity游戏:一跳到底——开始场景制作、圆柱随机旋转和移动、按钮功能的实现
1、开始场景制作
1、新建场景命名为Main
2、场景大小和游戏场景一样给场景添加背景图片和开始按钮
3、将场景加载拖入箭头所指中去
2、圆柱的随机旋转和移动
1、给圆柱所在空物体添加Box Collider组件并给空物体添加标签Pipe(图中最大绿色长方体)
2、两个圆柱都有
3、代码编写圆柱随机旋转和无限下移
private void OnTriggerEnter(Collider col)
{
// 管子移动给管子加了box Collider组件
if (col.tag == "Pipe")
{
//随机绕着Y轴旋转
float y = Random.Range(-90, 90);
transform.Rotate(new Vector3(0, y, 0));
//在自身位置上y轴的位置减去32得到无限向下产生(自己根据场景中的圆柱坐标进行设置)
col.transform.position = new Vector3(col.transform.position.x, col.transform.position.y - 32,
col.transform.position.z);
}
}
3、按钮功能的实现
1 代码编写新建两个脚本名字为Exit和GameStart
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class Exit : MonoBehaviour
{
public void ExitGame()
{
//退出
EditorApplication.isPlaying = false;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor.SceneManagement;
public class GameStart : MonoBehaviour
{
public void Game()
{
//重新开始加载场景
EditorSceneManager.LoadScene(0);
// 游戏进行
Time.timeScale = 1;
}
}
2、按钮功能绑定(对应好)
找到代码中的方法
六、Unity游戏:一跳到底——添加背景图片、音乐效果、小球颜色拖尾效果
1、添加背景图片
2、音乐效果
1、背景音乐给相机添加相应组件(Play On Awake 和Loop要勾选)
2、弹跳音乐给小球添相应空间并拖拽音乐(Play On Awake 和Loop要不用勾选)
3、代码实现
//弹跳音乐
private AudioSource jumpMusic;
private void OnTriggerEnter(Collider col)
{
//小球碰到面板
if (col.tag == "Plane")
{
//小球向上运动
rg.velocity = Vector3.up * speed;
// 涂鸦生产
GameObject color = Instantiate(colorPrefab,
new Vector3(transform.position.x, col.transform.position.y+0.5f, transform.position.z)
, colorPrefab.transform.rotation);
Destroy(color,0.2f);
jumpMusic.Play();
}
if (col.tag == "Error")
{
//背景音乐暂停
GameObject.Find("Main Camera").GetComponent<AudioSource>().Stop();
// 按钮显示
start.gameObject.SetActive(true);
exit.gameObject.SetActive(true);
}
}