龙空技术网

【6.Physics和动画】9.场景间的跳转

青少年编程ABC 35

前言:

现时你们对“unity 跳跃动画怎么制作”大致比较注意,姐妹们都想要了解一些“unity 跳跃动画怎么制作”的相关内容。那么小编在网上汇集了一些对于“unity 跳跃动画怎么制作””的相关内容,希望你们能喜欢,大家一起来了解一下吧!

9.场景间的跳转9.1 场景跳转

Unity 的游戏画面是通过场景来组织的。比如一个游戏会具有如下场景:

主题场景菜单场景游戏场景通关场景结束场景

这些场景共同组成了一个游戏。从一个场景跳转到另一个场景需要指定相应的场景名称。

9.2 创建游戏的通关场景

在前面的几节中,我们的游戏只有游戏场景,没有其他场景,根据本游戏的功能设计,需要增加通关场景。接下来,我们增加通关场景。

将当前游戏场景保存后,选择 File -> New Scene 创建新场景,然后选择 File -> Save Scene as ,在弹出的界面中输入 EndScene 并保存。

在工程窗口中选择通关图片并拖拽到该场景中,在检视器窗口中将 Transform 项的 Position 的 X、Y、Z 均设置为 0 。

单击运行按钮,查看当前场景效果。

9.3 跳转到通关场景

当角色与旗帜发生碰撞时表示游戏结束,需要跳转到通关场景,我们可以在角色控制器中通过代码来实现,在角色控制器中,将碰撞检测事件 OnTriggerEnter2D 代码修改如下:

    void OnTriggerEnter2D(Collider2D other) {        SceneManager.LoadScene("EndScene");        Debug.Log("您已经到达目的地!");    }

注意:使用 SceneManager 需要引入:

using UnityEngine.SceneManagement;

启动游戏检查效果。此时,我们发现在游戏运行到最后没有跳转到通关场景,检测控制台,发现如下异常:

Scene 'EndScene' couldn't be loaded because it has not been added to the build settings or the AssetBundle has not been loaded.To add a scene to the build settings use the menu File->Build Settings...UnityEngine.SceneManagement.SceneManager:LoadScene(String)PlayerController:OnTriggerEnter2D(Collider2D) (at Assets/PlayerController.cs:51)

异常中提示要通过 File->Build Settings... 进行设置。这是因为通关场景没有被注册到 Unity 中,场景跳转时,场景必须先注册到 Unity 中。

9.4 注册场景

在菜单中选择 File -> Build Settings...,在 Build Settings... 界面中,从工程窗口中将场景拖拽到 Scene In Build 中,然后调整场景的顺序,场景的右侧显示 0、1 编号,游戏启动时将从 0 号场景启动,所以,调整场景序号,让 GameScene 序号为 0 ,EndScene 序号为 1。

再次启动游戏,看看效果。

9.5 从通关场景跳转到游戏场景

当游戏结束后,游戏画面位于通关场景,此时我们希望当单击后游戏重新开始,我们可以通过控制器来实现,创建控制器 EndController,并编写脚本如下:

using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.SceneManagement;public class EndController : MonoBehaviour{    // Start is called before the first frame update    void Start()    {            }    // Update is called once per frame    void Update()    {        if (Input.GetMouseButtonDown(0)) SceneManager.LoadScene("GameScene");    }}

在通关场景中创建一个空对象并挂载脚本,场景视图切换回游戏场景,然后启动游戏看看吧。

9.6 解决游戏中的问题

这个游戏在玩的过程中发现有一些问题:

跳跃过程中可以连续多次跳跃;角色掉到画面以外的时候会一直下落;

第一个问题在角色控制器中检查角色的速度,如果在 y 方向的速度为 0 则可以跳跃,y 方向的速度可以通过 Rigidbody 组件的 velocity.y 来获取;第二个问题可以通过在角色控制器中检查角色的 y 坐标,当 y 坐标小于 -10 时,让场景回到最初的状态。

所以,对角色控制器脚本修改为:

using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.SceneManagement;public class PlayerController : MonoBehaviour{    Rigidbody2D rigid2d;    float jumpForce = 850.0f;    float moveForce = 270.0f;    float maxSpeed = 2.0f;    Animator animator;    // Start is called before the first frame update    void Start()    {        rigid2d = GetComponent<Rigidbody2D>();        animator = GetComponent<Animator>();    }    // Update is called once per frame    void Update()    {        // 跳跃        if (Input.GetKeyDown(KeyCode.Space) && rigid2d.velocity.y == 0)             rigid2d.AddForce(transform.up * jumpForce);        // 左右移动        int key = 0;        if (Input.GetKeyDown(KeyCode.LeftArrow)) key = -1;        if (Input.GetKeyDown(KeyCode.RightArrow)) key = 1;        if (key != 0) Debug.Log(key);        // 获取角色的移动速度        float currentSpeed = Mathf.Abs(rigid2d.velocity.x);        if (key != 0 && currentSpeed < maxSpeed) {            // Debug.Log(currentSpeed);            rigid2d.AddForce(transform.right * key * moveForce);        }        // 角色朝向        if (key != 0) transform.localScale = new Vector3(key, 1, 1);        // 动画部分速度        animator.speed = currentSpeed / 2.0f;        // 回到最初状态        if (transform.position.y < -10) SceneManager.LoadScene("GameScene");    }    // 碰撞检测    void OnTriggerEnter2D(Collider2D other) {        SceneManager.LoadScene("EndScene");        Debug.Log("您已经到达目的地!");    }}

在上面的代码中,跳跃时增加了 rigid2d.velocity.y == 0 来检查角色的速度,在 Update 方法的最后,增加了 if (transform.position.y < -10) SceneManager.LoadScene("GameScene"); 来处理角色一直掉落。

标签: #unity 跳跃动画怎么制作 #unity跳转 #unity跳转场景不销毁 #unity触发区域跳转场景 #unity跳转场景代码的命名空间