[周更]4.unity3D_Roll_a_Ball
ε≡٩(๑>₃<)۶ 一心向学
前言
Roll a Ball是一个非常简单的小游戏,非常适合拿来给初学者练手,下图为实现的效果。

一、场景搭建
这里我们介绍相关物体的基础设置,这四个物体的贴图都可以直接在网上找到。
1.地板
地板的存在就是为了防止小球落下去,我们可以直接设置为一个平面。

记住这个平面必须有Mesh Collider组件,这个组件主要就是检测碰撞,防止小球直接掉下去。
2.墙
四个长方体搭在一起……。这四个也要有碰撞检测器。


3.金币
金币是用Cylinder做的,因为金币是我们要拾取的道具,所以它的碰撞修改有点不同。记住这里我们必须把is Trigger勾上,这是为了让小球既能流畅地通过金币,又能成功检测到金币。

4.球
这里我们给小球添加一个刚体组件,这是为了防止小球变形,且能在场景中与其他部件正常交互。同时为了防止小球跳出场景,小球需要有一定重力。

二、脚本设计
1. 小球的脚本
检测既要检测小球的碰撞,又要能通过键盘控制小球的移动。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RigidBall : MonoBehaviour
{
private Rigidbody rigidbody;
public float power;
// Start is called before the first frame update
void Start()
{
rigidbody = GetComponent<Rigidbody>();
}
// Update is called once per frame
private void Update()
{
//获取水平 垂直输入
float hor = Input.GetAxis("Horizontal");
float ver = Input.GetAxis("Vertical");
//使用刚体模拟小球滚动
rigidbody.AddForce(new Vector3(hor, 0, ver) * power);
if (Input.GetKeyDown(KeyCode.Space)&&transform.position.y<=0.5f)
{
//跳跃模拟
//rigidbody.AddForce(Vector3.up * power);
//设置向上速度
rigidbody.AddForce(new Vector3(0,10,0)*power);
}
}
//检测是否碰到了墙,碰到了墙就反弹,这个其实可有可无
//主要是为了模拟撞墙后的效果,如果没有这个检测,小球
//撞墙就会直接停下
private void OnCollisionEnter(Collision collision)
{
GameObject other = collision.gameObject;
switch (other.tag)
{
case "Wall":
float fFlag = -1.0f;
if (other.name == "WallUp")
{
float lAngle = Vector3.Angle(transform.up, Vector3.right);
transform.Rotate(Vector3.forward * 2.0f * lAngle * fFlag);
}
else if (other.name == "WallDown")
{
float lAngle = Vector3.Angle(transform.up, Vector3.right);
transform.Rotate(Vector3.forward * 2.0f * lAngle);
}
else if (other.name == "WallLeft")
{
float lAngle = Vector3.Angle(transform.up, Vector3.up);
transform.Rotate(Vector3.forward * 2.0f * lAngle * fFlag);
}
else if (other.name == "WallRight")
{
float lAngle = Vector3.Angle(transform.up, Vector3.up);
transform.Rotate(Vector3.forward * 2.0f * lAngle);
}
Debug.Log("进入" + collision.gameObject.name);
break;
default:
break;
}
}
#region 触发器碰撞检测函数,主要是为了检测小球撞到金币
//注意:两两碰撞必须至少有一方是触发器 必须有一方有刚体组件
// Update is called once per frame
private void OnTriggerEnter(Collider other)
{
Debug.Log("进入" + other.gameObject.name);
switch (other.tag)
{
case "coin":
GameObject.Destroy(other.gameObject);
break;
default:
break;
}
}
2. 金币的脚本
这个比较简单,我们只要让金币旋转就好了。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Coin : MonoBehaviour
{
// Start is called before the first frame update
//可以修改速度
public float speed=100;
private void Update()
{
//旋转移动,可有可无
transform.Rotate(Vector3.up * speed * Time.deltaTime, Space.World);
}
}
3. 摄像头的脚本
让摄像头跟随小球移动,防止小球离开视界。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Camera : MonoBehaviour
{
// Start is called before the first frame update
public Transform target;//摄像机跟随的目标
//偏移量
Vector3 offset;
private void Start()
{
//目标位置 - 摄像机位置
offset = target.position - transform.position;
}
private void LateUpdate()
{
//更新相机位置 保持偏移量不变
transform.position = target.position - offset;
}
}
注:写完这个后。记得到摄像头的设置界面,将组件设置成如下图所示的情况:
4. 金币持续生成的脚本
我们将这个脚本绑到平面上,这样金币就会不断生成了。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Coin_grow : MonoBehaviour
{
// Start is called before the first frame update
public float duration;
private float m_Time;
void Start()
{
m_Time = duration;
}
// Update is called once per frame
void Update()
{
m_Time -= Time.deltaTime;
}
//这个函数的意思是其将在每次Update函数调用之后调用一次
private void LateUpdate()
{
//每隔一段时间,就生成一个金币。
if (m_Time <= 0)
{
GameObject new_coin = GameObject.Instantiate(GameObject.FindGameObjectWithTag("coin"));
new_coin.transform.position = new Vector3(Random.Range(-14.5f,14.5f), 0.888f, Random.Range(-14.5f, 14.5f));
m_Time = duration;
}
}
}
总结
Roll_a_Ball只是一个非常简单的小游戏,也没有动画和音效,下周争取整个复杂点的出来。该项目所有的文件都在这了。提取码是73ns。
ᕦ(・ㅂ・)ᕤ(任何不足之处,欢迎大家留言)
本文介绍了Unity3D初学者教程,通过创建Roll a Ball小游戏来讲解场景搭建和脚本设计。包括地板、墙、金币和球的设置,以及小球、金币、摄像头和金币生成器的脚本实现,旨在帮助学习者掌握Unity基础操作和碰撞检测。

3079

被折叠的 条评论
为什么被折叠?



