unity——小球酷跑游戏制作
课堂课程记录——小球滚动
所有变量与物体名的命名原则都是见名知意
一、创建一个unity项目
二、Create所需3Dobject
1.Player
2.walls
三、添加属性:
1.添加在Player上
a.添加Rigidbody组件
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 updatevoid Start(){rd=gameObject.GetComponent<Rigidbody>();}// Update is called once per framevoid 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包含
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 updatevoid Start(){offset=gameObject.transform.position.x-player.transform.position.x;}// Update is called once per framevoid 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 updatevoid Start(){offset_camera=gameObject.transform.position.x-player.transform.position.x;}// Update is called once per framevoid 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变量赋值:
至此基本的小球滚动游戏就完成了。
继续上节课的内容:
4.将player的形状改为球形
左键选中player的属性:
将mesh属性由cube改为sphere
5.创建障碍预制体
a.先创建一个3D物体cube,将其命名为barrier。
b.在project的asset中创建prefab预制体文件
并将之前创建的barrier直接拖拽到prefab中。
若对prefab预制体的作用不理解的话,可访问如下链接:
预制体的制作与功能
若barrier物体变为蓝色,则创建成功。
6.随机生成障碍物
a.创建一个空物体,然后命名为barrierControl。
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 initializationvoid Start () {}// Update is called once per framevoid 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给消除,小球一下就掉下去了)。
b.右键选中trigger,在右侧的属性栏,移除Cube(Mesh Filter)和Mesh Renderer属性。
使trigger成为透明状态:
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 initializationvoid Start () {int i = Random.Range(0,barrierMaterial.Length);gameObject.GetComponent<Renderer>().material = barrierMaterial[i];}// Update is called once per framevoid Update () {}
}
9.碰到障碍物颜色提示
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.分数记录
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中添加如下代码进行分数增加
在Playcolorcontrol中添加分数减少代码:
完整画面如下:
以上小球酷跑课程内容就结束了,稍后我会对游戏进行进一步的完善功能。
unity——小球酷跑游戏制作相关推荐
- Untity小球酷跑游戏制作过程
Untity小球酷跑游戏制作过程 一创建一个项目 然后鼠标右键点击3D object 创建游戏中所需要的模型. 下面是我所建的几个模型 然后将下面视图比列调整为16:10 第二步 设置小球属性 这里为 ...
- 小球酷跑游戏制作过程
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文 前言 制作小球酷跑 提示:以下是本篇文章正文内容,下面案例可供参考 一.制作实验场景 设计背景颜色: 二.创建代码 1.摄像机代码 代码 ...
- unity小球酷跑项目
[小球酷跑项目] 项目内容: 1,搭建游戏环境,添加刚体属性,控制小球跳跃,移动.2,设置相机角度,游戏背景,设置上下板的跟随移动和相机跟随.3,设置障碍物的生成,控制障碍物生成的大小,消除已经跨过的 ...
- unity小球酷跑(删减版)
一.创建物体... 1 二.属性... 2 三.给物体添加必要的参考物体... 3 四.键盘控制物体player运动的方法及项目C#脚本.... 5 一.创建物体 1.在Main Camera那一栏的 ...
- 简单的酷跑游戏制作思路
酷跑游戏是比较简单的,一个好的酷跑游戏,主要在于游戏场景的色调配置,以及动画制作的效果.酷跑游戏的简单的说就是手势的检测. 下面是我的在游戏物体上绑定的两个脚本 对手势检测的脚本 using Unit ...
- 小球酷跑(制作过程)
Part One. 1.环境搭建 打开unity,先把坐标系设置成仅有y轴和x轴,完成后要记得按Ctrl+S保存.如下图所示. 创建两个cube分别命名为wallUp和wallDown,并设置相等的大 ...
- 接上一篇:小球酷跑流程。项目代码
unity小球酷跑 流程链接: unity小球酷跑(删减版)_雨木目qq的博客-CSDN博客 https://blog.csdn.net/qq_53603060/article/details/124 ...
- 小球酷跑unity制作
小球酷跑unity **1.环境搭建和移动就是两个长方体中间夹着一个小球然后让小球实现上下移动以及自动向右边行驶的代码如下 ** using System.Collections; using Sys ...
- Unity 3D 入门小游戏 小球酷跑(下)
文章目录 一.障碍物自动生成 二.障碍物自动销毁 三.障碍物颜色随机组 四.碰到障碍物颜色提示 五.分数 总结 一.障碍物自动生成 为了保证游戏结束之前有源源不断的障碍物生成,所以要实现随机生成位置不 ...
- unity学习小球酷跑
unity学习小球酷跑 一.创建两个cube,在3dboject里,拉好合适的长度. 二.用相机选择一个背景 选择纯色,在选一个颜色. 最后的效果如上. 三.创建一个小球 在3d object里创建一 ...
最新文章
- 一些改进模型速度/精度的工程方法
- 在excel中如何增加组合框──EXCEL VBA的使用
- ubuntu开启mysql日志记录
- 统计学习方法 第一章 学习心得
- SAP BOPF draft table automatic deletion
- 人脸识别撞脸名画_艺术与时尚结合的极致——当服装设计遇到名画
- linux dd 光标在闪,linux dd详解
- 77种互联网盈利创新模式(7)
- 使用JavaFX打开fxml,找不到打开的图形界面
- tongweb自动部署_用apache配置TongWeb集群
- 《浪潮之巅》读书笔记
- Java实现音频格式转换 WAV—mp3,可使音频压缩
- 解决deep freeze冰点还原软件无法冻结的问题:计算机正在完成Deep Freeze冰点检测到的待定Windows更新
- Spring Cloud 系列之 Netflix Eureka 注册中心(一)
- Structure SLAM 相关论文阅读(一):消影点/消失点/灭点检测提取
- 密码找回、带星号密码查看解决方案
- 游戏《一战封神》副本星宿神殿挑战攻略
- LM09丨费雪逆变换反转网格策略
- 【98期】面试官:给我说说你对Java GC机制的理解?
- word格式角落的直角问题又名裁剪标记
热门文章
- 2008服务器安全修复,Windows 2008操作系统漏洞临时修复方法
- ARP 项添加失败: 请求的操作需要提升
- JavaScript屏蔽Backspace键
- R.Koo 改良后的分页类(主要是显示属性)
- 设置自动清理mysql binlog日志_自动清理MySQL binlog日志
- 必看!软考系统架构设计师考试详情与备考攻略
- 火车采集器V2010免费版下载
- 淘宝 NPM 镜像 node删除node_modules WebStorm license server address
- 阿里百川 WKWebView 无法拦截URL
- 当我们谈注册中心时我们谈什么