HOME> 国足世界杯夺冠> 【Unity3d】 教会你如何做一个简单的电梯系统(升降平台)

【Unity3d】 教会你如何做一个简单的电梯系统(升降平台)

博主第一次写博客,语言略俗,有不足之处还请指正!

由于自己还处在unity小白阶段,受2d升降平台的影响(后续我也会上传关于2d升降平台的文章),突发奇想如何用3d做一个电梯系统,查阅网上资料后,发现网上对这方面的讲解少之又少,或者说其他博主提供的并非自己想要的效果,博主也是不断地学习改进,最终才达到效果,所以想和大家分享一下我的学习成果,供大家学习参考。

如果你正在学习unity,会发现其实做一个简单的电梯系统很快就有思路,无非就是去触发Trigger,通过电梯移动实现到达目标楼层,之前也有疑惑和网友交流过到底用Collider碰撞器还是Trigger触发器去实现,这两种都是检测碰撞,个人觉得Trigger实现起来简单点,还有一个思路可能你会想到,在各个楼层安装Collider,当电梯到达某楼层通过检测碰撞,来置停电梯(本文不涉及,想实现的小伙伴可以自己尝试)。

本篇文章主要介绍我制作简单电梯过程(不包括开关门,如果确切划分也可以说是升降平台),博主的想法是一个电梯,按下几楼,电梯会将我们带到几楼,由于博主建模简单,所以利用sphere(球体)代替角色,cube代替电梯(如下图),采用踩Trigger实现前往目标楼层。

第一步,设计小球的运动,包括前后左右跳跃,摄像机部分省略(资源丰富),给小球加上Character Controller组件,新建C#文件,命名为PlayerController,通过中文API搜CharacterController.Move(为GameObject的移动提供附加组件),将其代码复制给PlayController.cs文件,便可实现小球的运动(参数可自己修改)。

第二步,设计电梯的运动,无非就是给电梯贴代码,给Cube(可自由命名,以下均已Cube为例)加上Box Collider以及Rigidbody,勾选Is Kinematic,并锁定X轴和Z轴,新建C#文件,命名为LiftController,代码如下(MoveTowards函数可查阅中文API):

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class LiftController : MonoBehaviour

{

public Collider coll;

public Rigidbody rigid;

public float speed;

Vector3 OneLayer = new Vector3(0f, 0.1f, 0f);//一楼,实际情况根据自己修改,以下同理

Vector3 TwoLayer = new Vector3(0f, 11.1f, 0f);//二楼

Vector3 ThreeLayer = new Vector3(0f, 21.1f, 0f);//三楼

void Start()

{

coll = gameObject.GetComponent();

rigid = gameObject.GetComponent();

}

void Update()

{

}

public void One()

{

transform.position = Vector3.MoveTowards(transform.position, OneLayer, speed * Time.deltaTime);

}

public void Two()

{

transform.position = Vector3.MoveTowards(transform.position, TwoLayer, speed * Time.deltaTime);

}

public void Three()

{

transform.position = Vector3.MoveTowards(transform.position, ThreeLayer, speed * Time.deltaTime);

}

}

第三步,在Cube下新建三个空物体,加上Box Collider,并勾选Is Trigger,在(Inspector)检视面板Tag下新增三个标签,分别取名为One、Two、Three,并分别为其勾选上,其次可以分别改变三个空物体的Scale(X 0.2 ,Y 0.3 ,Z 0.5),并通过移动合理分配位置,为了便于角色判断楼层,博主分别为三个空物体加上了Text(TMP),添加文字,调节即可,这样我们三个触发器就完成了。

第四步,完善代码,实现触发器,在PlayController.cs中进行添加,PlayController.cs代码如下:

using JetBrains.Annotations;

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class PlayerController : MonoBehaviour

{

private CharacterController controller;

private Vector3 playerVelocity;

private bool groundedPlayer;

public float playerSpeed;

public float jumpHeight;

public float gravityValue;

/*以上为角色运动定义*/

public GameObject Lift;

public bool isoneColl = false;

public bool istwoColl = false;

public bool isthreeColl = false;

/*以上定义便于判断是否触发*/

void Start()

{

controller = gameObject.GetComponent();

Lift = GameObject.Find("Lift");//引号内容为Cube的命名,可根据自己的改动

}

void Update()

{

Movement();

LiftMove();

}

//角色移动

void Movement()

{

groundedPlayer = controller.isGrounded;

if (groundedPlayer && playerVelocity.y < 0)

{

playerVelocity.y = 0f;

}

Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));

controller.Move(move * Time.deltaTime * playerSpeed);

if (move != Vector3.zero)

{

gameObject.transform.forward = move;

}

// Changes the height position of the player..

if (Input.GetButton("Jump") && groundedPlayer)

{

playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);

}

playerVelocity.y += gravityValue * Time.deltaTime;

controller.Move(playerVelocity * Time.deltaTime);

}

//触发器

private void OnTriggerEnter(Collider collision)

{

if (collision.tag == "One")

{

isoneColl = true;

istwoColl = false;

isthreeColl = false;

Debug.Log("前往1楼");

}

if (collision.tag == "Two")

{

istwoColl = true;

isoneColl = false;

isthreeColl = false;

Debug.Log("前往2楼");

}

if (collision.tag == "Three")

{

isthreeColl = true;

istwoColl = false;

isoneColl = false;

Debug.Log("前往3楼");

}

}

//电梯移动

void LiftMove()

{

if (isoneColl == true)

{

Lift.GetComponent().One();//调用函数

Debug.Log("移动中...");

}

if (istwoColl == true)

{

Lift.GetComponent().Two();//调用函数

Debug.Log("移动中...");

}

if (isthreeColl == true)

{

Lift.GetComponent().Three();//调用函数

Debug.Log("移动中...");

}

}

}

运行之前,别忘了给电梯设置速度哦(博主设置为3),谢谢大家!