课堂课程记录——小球滚动
所有变量与物体名的命名原则都是见名知意
一、创建一个unity项目
二、Create所需3Dobject
1.Player
2.walls
![在这里插入图片描述](https://img-blog.csdnimg.cn/aa21a5eba2214b9bae7382fa941c8c0d.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YmN6L-b55qE5bCP6YeO,size_17,color_FFFFFF,t_70,g_se,x_16)
三、添加属性:
1.添加在Player上
a.添加Rigidbody组件![在这里插入图片描述](https://img-blog.csdnimg.cn/37e8cac9f54a4a269288b34c6ee84c58.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YmN6L-b55qE5bCP6YeO,size_12,color_FFFFFF,t_70,g_se,x_16)
b.添加new script组件,并命名为PlayMove,代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playMove : MonoBehaviour
{
public Rigidbody rd;
public float speadAutoMove=5;
public float speadMoveUpandDown=20;
// Start is called before the first frame update
void Start()
{
rd=gameObject.GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
PlayerAutoMove();
PlayerMoveUpandDown();
}
private void PlayerAutoMove(){
rd.AddForce(Vector3.right*speadAutoMove); //前进
}
private void PlayerMoveUpandDown()
{
float v=Input.GetAxis("Vertical"); //上下
rd.AddForce(v*Vector3.up*speadMoveUpandDown);//给一个上下的力量
}
}
2.添加到walls上
a.首先create empty将wall包含
![在这里插入图片描述](https://img-blog.csdnimg.cn/6954f41f709b487b808859ebba8204d1.png)
b.在Wall上添加new script组件,代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class wallControl : MonoBehaviour
{
private float offset;
public GameObject player;
// Start is called before the first frame update
void Start()
{
offset=gameObject.transform.position.x-player.transform.position.x;
}
// Update is called once per frame
void Update()
{
FollowPlayMove();
}
void FollowPlayMove(){
gameObject.transform.position=new Vector3(player.transform.position.x+offset,0,0);
}
}
3.实现相机跟随
a.在相机上添加new script 组件并命名为cameraControl,代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cameraControl : MonoBehaviour
{
public GameObject player;
private float offset_camera;
// Start is called before the first frame update
void Start()
{
offset_camera=gameObject.transform.position.x-player.transform.position.x;
}
// Update is called once per frame
void Update()
{
FollowCameraMove();
}
void FollowCameraMove(){
gameObject.transform.position=new Vector3(offset_camera+player.transform.position.x,gameObject.transform.position.y,gameObject.transform.position.z);
}
}
b.将script中设置的player变量赋值:
![在这里插入图片描述](https://img-blog.csdnimg.cn/c3c28eed534d4c989f79d20968f0fa10.png)
至此基本的小球滚动游戏就完成了。
![在这里插入图片描述](https://img-blog.csdnimg.cn/d7c6a3313d1d4d29a6a2063156e74abe.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5YmN6L-b55qE5bCP6YeO,size_20,color_FFFFFF,t_70,g_se,x_16)
继续上节课的内容:
4.将player的形状改为球形
左键选中player的属性:![在这里插入图片描述](https://img-blog.csdnimg.cn/68b4264af9af4f799d8ef414c83856f2.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAR08hISE=,size_17,color_FFFFFF,t_70,g_se,x_16)
将mesh属性由cube改为sphere
5.创建障碍预制体
a.先创建一个3D物体cube,将其命名为barrier。
b.在project的asset中创建prefab预制体文件
![在这里插入图片描述](https://img-blog.csdnimg.cn/cccdac6e12fe4be38dfa6e85ca50f9d5.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAR08hISE=,size_20,color_FFFFFF,t_70,g_se,x_16)
并将之前创建的barrier直接拖拽到prefab中。
若对prefab预制体的作用不理解的话,可访问如下链接:
预制体的制作与功能
若barrier物体变为蓝色,则创建成功。
![在这里插入图片描述](https://img-blog.csdnimg.cn/40b678a640744ccb8cf56fc21cbb97a2.png)
6.随机生成障碍物
a.创建一个空物体,然后命名为barrierControl。
![在这里插入图片描述](https://img-blog.csdnimg.cn/f31a28e788214a718ae502b2b2af2e78.png)
b.在该物体上添加new script的组件,C#代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BarrierControl : MonoBehaviour {
public int barrierInterval=5;
public GameObject player;
public GameObject CurrentBarrier;
public GameObject BarrierPre;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
AutoCreatBarrier();
}
// 障碍物自动生成
public void AutoCreatBarrier()
{
if(player.transform.position.x>CurrentBarrier.transform.position.x)
{
//生成新的障碍物
float targetX = CurrentBarrier.transform.position.x + barrierInterval;
float targetY = RandomBarrierPosition();
Vector3 targetPos = new Vector3(targetX,targetY,0);
GameObject g = Instantiate(BarrierPre,targetPos,Quaternion.identity);
//随机大小
g.transform.localScale = new Vector3(g.transform.localScale.x, RandomBarrierSize((int)g.transform.position.y), g.transform.localScale.z);
//判断障碍更换
CurrentBarrier = g;
}
}
//障碍随机大小
public float RandomBarrierSize(int r)
{
int rAbs = Mathf.Abs(r);
if(rAbs==0)
{
return 6;
}
else
{
return (3-rAbs)*2+1;
}
}
//障碍物随机位置
public float RandomBarrierPosition()
{
int r = Random.Range(-3,3);
Debug.Log(r);
return r;
}
}
到目前为止障碍物就能不断的在与小球的距离控制下产生了。
7.障碍的清除
a.在之前的wall文件夹中创建一个新cube物体,命名为trigger,控制大小长度在略小于上下wall之间,以便过滤掉与其接触的barrier(切记不要接触上下的wall,否则游戏一开始就会将上下的wall给消除,小球一下就掉下去了)。
![在这里插入图片描述](https://img-blog.csdnimg.cn/b5c43b51a11042b6993e7476986fb25e.png)
b.右键选中trigger,在右侧的属性栏,移除Cube(Mesh Filter)和Mesh Renderer属性。
![在这里插入图片描述](https://img-blog.csdnimg.cn/93645d1e083746adae25baca821a504e.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAR08hISE=,size_14,color_FFFFFF,t_70,g_se,x_16)
使trigger成为透明状态:
![在这里插入图片描述](https://img-blog.csdnimg.cn/f5fb207573e1452a8dd06b9b967790ca.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAR08hISE=,size_15,color_FFFFFF,t_70,g_se,x_16)
c.给trigger添加new script组件,C#代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AutoDestoryBarriers : MonoBehaviour {
private void OnTriggerEnter(Collider other)
{
Destroy(other.gameObject);
}
}
8.给障碍物添加随机产生颜色功能:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BarrierColor : MonoBehaviour {
public Material[] barrierMaterial;
// Use this for initialization
void Start () {
int i = Random.Range(0,barrierMaterial.Length);
gameObject.GetComponent<Renderer>().material = barrierMaterial[i];
}
// Update is called once per frame
void Update () {
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/46db65af57c04a4dbcdabbf4643b3a0a.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAR08hISE=,size_20,color_FFFFFF,t_70,g_se,x_16)
9.碰到障碍物颜色提示
![在这里插入图片描述](https://img-blog.csdnimg.cn/f551301ef1164bedaa3d3709563c5420.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAR08hISE=,size_20,color_FFFFFF,t_70,g_se,x_16)
C#代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerColorControl : MonoBehaviour
{
private void OnCollisionEnter(Collision collision)
{
// Debug.Log("1");
//分数减少
UIcontrol._instance.AddScore(-10);
gameObject.GetComponent<Renderer>().material.color=Color.red;
}
private void OnCollisionExit(Collision collision)
{
gameObject.GetComponent<Renderer>().material.color=Color.white;
}
}
9.分数记录
![在这里插入图片描述](https://img-blog.csdnimg.cn/5f66bcbfc02d4d12bda48f34cc080f35.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAR08hISE=,size_20,color_FFFFFF,t_70,g_se,x_16)
C#代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIcontrol : MonoBehaviour
{
public Text scoreText;
public int score=0;
//单列模式
public static UIcontrol _instance;
private void Awake()
{
_instance=this;
}
public void AddScore(int x)
{
score+=x;
scoreText.text="得分:"+score;
}
}
并在barrierControl中添加如下代码进行分数增加
![在这里插入图片描述](https://img-blog.csdnimg.cn/1046a1158cf94358a6932de143f94042.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAR08hISE=,size_20,color_FFFFFF,t_70,g_se,x_16)
在Playcolorcontrol中添加分数减少代码:
![在这里插入图片描述](https://img-blog.csdnimg.cn/89e5d2fb16044858ad75b5b13745c365.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAR08hISE=,size_20,color_FFFFFF,t_70,g_se,x_16)
完整画面如下:
![在这里插入图片描述](https://img-blog.csdnimg.cn/c23e279768ca4540a9b2bd5ba70aa194.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAR08hISE=,size_20,color_FFFFFF,t_70,g_se,x_16)
以上小球酷跑课程内容就结束了,稍后我会对游戏进行进一步的完善功能。