• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Unity3d 教会你做的电梯系统升降平台

武飞扬头像
樱花不再在海棠湾涮羊肉
帮助1

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

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

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

学新通

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

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

  1.  
    using System.Collections;
  2.  
    using System.Collections.Generic;
  3.  
    using UnityEngine;
  4.  
     
  5.  
    public class LiftController : MonoBehaviour
  6.  
    {
  7.  
    public Collider coll;
  8.  
    public Rigidbody rigid;
  9.  
    public float speed;
  10.  
     
  11.  
    Vector3 OneLayer = new Vector3(0f, 0.1f, 0f);//一楼,实际情况根据自己修改,以下同理
  12.  
    Vector3 TwoLayer = new Vector3(0f, 11.1f, 0f);//二楼
  13.  
    Vector3 ThreeLayer = new Vector3(0f, 21.1f, 0f);//三楼
  14.  
     
  15.  
    void Start()
  16.  
    {
  17.  
    coll = gameObject.GetComponent<Collider>();
  18.  
    rigid = gameObject.GetComponent<Rigidbody>();
  19.  
     
  20.  
    }
  21.  
     
  22.  
    void Update()
  23.  
    {
  24.  
     
  25.  
    }
  26.  
     
  27.  
    public void One()
  28.  
    {
  29.  
    transform.position = Vector3.MoveTowards(transform.position, OneLayer, speed * Time.deltaTime);
  30.  
    }
  31.  
    public void Two()
  32.  
    {
  33.  
    transform.position = Vector3.MoveTowards(transform.position, TwoLayer, speed * Time.deltaTime);
  34.  
    }
  35.  
     
  36.  
    public void Three()
  37.  
    {
  38.  
    transform.position = Vector3.MoveTowards(transform.position, ThreeLayer, speed * Time.deltaTime);
  39.  
    }
  40.  
    }
学新通

       第三步,在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代码如下:

  1.  
    using JetBrains.Annotations;
  2.  
    using System.Collections;
  3.  
    using System.Collections.Generic;
  4.  
    using UnityEngine;
  5.  
     
  6.  
    public class PlayerController : MonoBehaviour
  7.  
    {
  8.  
    private CharacterController controller;
  9.  
    private Vector3 playerVelocity;
  10.  
    private bool groundedPlayer;
  11.  
    public float playerSpeed;
  12.  
    public float jumpHeight;
  13.  
    public float gravityValue;
  14.  
    /*以上为角色运动定义*/
  15.  
     
  16.  
    public GameObject Lift;
  17.  
    public bool isoneColl = false;
  18.  
    public bool istwoColl = false;
  19.  
    public bool isthreeColl = false;
  20.  
    /*以上定义便于判断是否触发*/
  21.  
     
  22.  
    void Start()
  23.  
    {
  24.  
    controller = gameObject.GetComponent<CharacterController>();
  25.  
    Lift = GameObject.Find("Lift");//引号内容为Cube的命名,可根据自己的改动
  26.  
    }
  27.  
     
  28.  
    void Update()
  29.  
    {
  30.  
    Movement();
  31.  
    LiftMove();
  32.  
    }
  33.  
     
  34.  
    //角色移动
  35.  
    void Movement()
  36.  
    {
  37.  
    groundedPlayer = controller.isGrounded;
  38.  
    if (groundedPlayer && playerVelocity.y < 0)
  39.  
    {
  40.  
    playerVelocity.y = 0f;
  41.  
    }
  42.  
     
  43.  
    Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
  44.  
    controller.Move(move * Time.deltaTime * playerSpeed);
  45.  
     
  46.  
    if (move != Vector3.zero)
  47.  
    {
  48.  
    gameObject.transform.forward = move;
  49.  
    }
  50.  
     
  51.  
    // Changes the height position of the player..
  52.  
    if (Input.GetButton("Jump") && groundedPlayer)
  53.  
    {
  54.  
    playerVelocity.y = Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
  55.  
    }
  56.  
     
  57.  
    playerVelocity.y = gravityValue * Time.deltaTime;
  58.  
    controller.Move(playerVelocity * Time.deltaTime);
  59.  
    }
  60.  
     
  61.  
    //触发器
  62.  
    private void OnTriggerEnter(Collider collision)
  63.  
    {
  64.  
    if (collision.tag == "One")
  65.  
    {
  66.  
    isoneColl = true;
  67.  
    istwoColl = false;
  68.  
    isthreeColl = false;
  69.  
    Debug.Log("前往1楼");
  70.  
    }
  71.  
    if (collision.tag == "Two")
  72.  
    {
  73.  
    istwoColl = true;
  74.  
    isoneColl = false;
  75.  
    isthreeColl = false;
  76.  
    Debug.Log("前往2楼");
  77.  
    }
  78.  
    if (collision.tag == "Three")
  79.  
    {
  80.  
    isthreeColl = true;
  81.  
    istwoColl = false;
  82.  
    isoneColl = false;
  83.  
    Debug.Log("前往3楼");
  84.  
    }
  85.  
     
  86.  
    }
  87.  
     
  88.  
    //电梯移动
  89.  
    void LiftMove()
  90.  
    {
  91.  
    if (isoneColl == true)
  92.  
    {
  93.  
    Lift.GetComponent<LiftController>().One();//调用函数
  94.  
    Debug.Log("移动中...");
  95.  
    }
  96.  
    if (istwoColl == true)
  97.  
    {
  98.  
    Lift.GetComponent<LiftController>().Two();//调用函数
  99.  
    Debug.Log("移动中...");
  100.  
    }
  101.  
    if (isthreeColl == true)
  102.  
    {
  103.  
    Lift.GetComponent<LiftController>().Three();//调用函数
  104.  
    Debug.Log("移动中...");
  105.  
    }
  106.  
    }
  107.  
     
  108.  
    }
学新通

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

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgffhik
系列文章
更多 icon
同类精品
更多 icon
继续加载