? ? ? ?博主第一次寫博客,語言略俗,有不足之處還請指正!
? ? ? ?由于自己還處在unity小白階段,受2d升降平臺的影響(后續(xù)我也會上傳關于2d升降平臺的文章),突發(fā)奇想如何用3d做一個電梯系統(tǒng),查閱網(wǎng)上資料后,發(fā)現(xiàn)網(wǎng)上對這方面的講解少之又少,或者說其他博主提供的并非自己想要的效果,博主也是不斷地學習改進,最終才達到效果,所以想和大家分享一下我的學習成果,供大家學習參考。
? ? ? ?如果你正在學習unity,會發(fā)現(xiàn)其實做一個簡單的電梯系統(tǒng)很快就有思路,無非就是去觸發(fā)Trigger,通過電梯移動實現(xiàn)到達目標樓層,之前也有疑惑和網(wǎng)友交流過到底用Collider碰撞器還是Trigger觸發(fā)器去實現(xiàn),這兩種都是檢測碰撞,個人覺得Trigger實現(xiàn)起來簡單點,還有一個思路可能你會想到,在各個樓層安裝Collider,當電梯到達某樓層通過檢測碰撞,來置停電梯(本文不涉及,想實現(xiàn)的小伙伴可以自己嘗試)。
? ? ? ?本篇文章主要介紹我制作簡單電梯過程(不包括開關門,如果確切劃分也可以說是升降平臺),博主的想法是一個電梯,按下幾樓,電梯會將我們帶到幾樓,由于博主建模簡單,所以利用sphere(球體)代替角色,cube代替電梯(如下圖),采用踩Trigger實現(xiàn)前往目標樓層。
? ? ? ?第一步,設計小球的運動,包括前后左右跳躍,攝像機部分省略(資源豐富),給小球加上Character Controller組件,新建C#文件,命名為PlayerController,通過中文API搜CharacterController.Move(為GameObject的移動提供附加組件),將其代碼復制給PlayController.cs文件,便可實現(xiàn)小球的運動(參數(shù)可自己修改)。
? ? ? ?第二步,設計電梯的運動,無非就是給電梯貼代碼,給Cube(可自由命名,以下均已Cube為例)加上Box Collider以及Rigidbody,勾選Is Kinematic,并鎖定X軸和Z軸,新建C#文件,命名為LiftController,代碼如下(MoveTowards函數(shù)可查閱中文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);//一樓,實際情況根據(jù)自己修改,以下同理
Vector3 TwoLayer = new Vector3(0f, 11.1f, 0f);//二樓
Vector3 ThreeLayer = new Vector3(0f, 21.1f, 0f);//三樓
void Start()
{
coll = gameObject.GetComponent<Collider>();
rigid = gameObject.GetComponent<Rigidbody>();
}
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,并分別為其勾選上,其次可以分別改變?nèi)齻€空物體的Scale(X 0.2 ,Y 0.3 ,Z 0.5),并通過移動合理分配位置,為了便于角色判斷樓層,博主分別為三個空物體加上了Text(TMP),添加文字,調(diào)節(jié)即可,這樣我們?nèi)齻€觸發(fā)器就完成了。
? ? ? ?第四步,完善代碼,實現(xiàn)觸發(fā)器,在PlayController.cs中進行添加,PlayController.cs代碼如下:文章來源:http://www.zghlxwxcb.cn/news/detail-515670.html
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;
/*以上定義便于判斷是否觸發(fā)*/
void Start()
{
controller = gameObject.GetComponent<CharacterController>();
Lift = GameObject.Find("Lift");//引號內(nèi)容為Cube的命名,可根據(jù)自己的改動
}
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);
}
//觸發(fā)器
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<LiftController>().One();//調(diào)用函數(shù)
Debug.Log("移動中...");
}
if (istwoColl == true)
{
Lift.GetComponent<LiftController>().Two();//調(diào)用函數(shù)
Debug.Log("移動中...");
}
if (isthreeColl == true)
{
Lift.GetComponent<LiftController>().Three();//調(diào)用函數(shù)
Debug.Log("移動中...");
}
}
}
? ? ? ? 運行之前,別忘了給電梯設置速度哦(博主設置為3),謝謝大家!文章來源地址http://www.zghlxwxcb.cn/news/detail-515670.html
到了這里,關于【Unity3d】 教會你如何做一個簡單的電梯系統(tǒng)(升降平臺)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!