unity实现第一人称场景漫游项目,主要包含如下功能:

1、场景搭建

场景主要包括水、草、树木、石头、飘动的白云,房子等。

2、模型导入

这个场景就导入了一个桥和一个房子,这个桥横跨在小溪的两边,我们可以通过桥从小溪这边走到对面去。

3、粒子特效

在这个项目里面主要使用了烟特效,把这个烟特效放到房子的房顶模拟炊烟特别好。还有3个火焰的粒子特效,一个火焰模拟火堆效果,还有两个火焰特效作为桥上面的照明效果,整体效果特别好看。还可以继续扩展,添加喷泉等粒子特效。

4、第一人称控制

主要就是通过控制摄像机进行视角的控制,通过WASD控制前后左右的移动。

5、添加音效

怎么能少了音效呢,哈哈哈,得有点氛围吧。

6、添加小地图

显示玩家在场景当中的位置。

7、等等。

整体创造了一个

小桥流水人家

行云流水

炊烟冉冉升起

火焰燃烧

的一片大好世外桃源之地。

步骤如下:

  1. 首先就是制作一个房子了,我的房子不是特别大,不过麻雀虽小五脏俱全,基本东西都是有的,效果如下图:

unity虚拟现实技术场景漫游_unity

unity虚拟现实技术场景漫游_unity_02

unity虚拟现实技术场景漫游_unity_03

unity虚拟现实技术场景漫游_unity_04

unity虚拟现实技术场景漫游_unity_05

unity虚拟现实技术场景漫游_unity_06

基本上房子里面的效果就是这样了。

  1. 有了房子以后就可以添加地形了,制作一个包含天空、云、水、山、草、树木、桥的环境。此美景堪称“行云流水”,“小桥流水人家”,“夕阳西下”。

如下图:

unity虚拟现实技术场景漫游_unity_07

unity虚拟现实技术场景漫游_unity_08

unity虚拟现实技术场景漫游_unity_09

  1. 场景和房子都有了,是时候把我们的主角请出来了,做一个第一人称视角的漫游。主要就是控制摄像机的旋转和前后左右移动。

unity虚拟现实技术场景漫游_unity_10

核心代码如下:

控制摄像机旋转。

using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class CameraTurn : MonoBehaviour

{

    // 水平视角移动的敏感度

    public float sensitivityHor = 3f;

    // 垂直视角移动的敏感度

    public float sensitivityVer = 3f;

    // 视角向上移动的角度范围,该值越小范围越大

    public float upVer = -40;

    // 视角向下移动的角度范围,该值越大范围越大

    public float downVer = 45;

    // 垂直旋转角度

    private float rotVer;


    // 旋转的方向问题

    // x 表示绕 x 轴旋转,即 前上后 的角度

    // y 表示绕 y 轴旋转,即 左前后 的角度

    // y 表示绕 y 轴旋转,即 左前后 的角度


    // Start is called before the first frame update

    void Start()

    {

        // 初始化当前的垂直角度

        rotVer = transform.eulerAngles.x;

    }


    // Update is called once per frame

    void Update()

    {

        // 获取鼠标上下的移动位置

        float mouseVer = Input.GetAxis("Mouse Y");

        // 获取鼠标左右的移动位置

        float mouseHor = Input.GetAxis("Mouse X");

        // 鼠标往上移动,视角其实是往下移,所以要想达到视角也往上移的话,就要减去它

        rotVer -= mouseVer * sensitivityVer;

        // 限定上下移动的视角范围,即垂直方向不能360度旋转

        rotVer = Mathf.Clamp(rotVer, upVer, downVer);

        // 水平移动

        float rotHor = transform.localEulerAngles.y + mouseHor * sensitivityHor;

        // 设置视角的移动值

        transform.localEulerAngles = new Vector3(rotVer, rotHor, 0);

    }

}

控制移动:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// 表示一定需要这个控件
[RequireComponent(typeof(CharacterController))]
public class Move : MonoBehaviour
{
    // 获取摄像机对象的位置信息,[SerializeField] 类似于 java 的构造方法的方法参数
    [SerializeField] private Transform target;
    // 跳起来的速度
    public float jumpSpeed = 15.0f;
    // 重力
    public float gravity = -9.8f;
    // 最终垂直速度
    public float endsVelocity = -10.0f;
    // 在地面时的垂直速度
    public float minFall = -1.5f;
    // 跑起来的速度
    public float runSpeed = 10;
    // 走路的速度
    public float walkSpeed = 4;
    // 垂直速度
    private float verSpeed;

    // 移动的速度
    private float moveSpeed;
    // 用于存储当前的角色控件
    private CharacterController character;

    // 在被加载时执行
    void Start()
    {
        // 初始化
        character = GetComponent<CharacterController>();
        verSpeed = minFall;
        moveSpeed = walkSpeed;
    }
    // 每更新一帧时执行
    void Update()
    {
        // 用于存储移动信息
        Vector3 movement = Vector3.zero;
        // 获取左右方向的移动信息
        float horspeed = Input.GetAxis("Horizontal");
        // 获取前后方向的移动信息
        float verspeed = Input.GetAxis("Vertical"); 
        // 当发生了移动才执行
        if (horspeed != 0 || verspeed != 0)
        {
            // 设置左右位置
            movement.x = horspeed * moveSpeed;
            // 设置前后的位置
            movement.z = verspeed * moveSpeed;
            // 设置斜着走的最大速度更水平垂直走的速度一样
            movement = Vector3.ClampMagnitude(movement, moveSpeed);
            // 将移动的信息转化为以摄像机为全局坐标的位置,即保证你向前走一定是摄像机的视角方向
            movement = target.TransformDirection(movement);
        }
        // 当按下左 shift 是跟换速度
        if (Input.GetKey(KeyCode.LeftShift))
        {
            moveSpeed = runSpeed;
        }
        else
        {
            moveSpeed = walkSpeed;
        }
        // 角色控件自带的一个方法,用于检测是否在地面
        if (character.isGrounded)
        {
            // 按了空格键则给垂直方向施加一个速度
            if (Input.GetButtonDown("Jump"))
            {
                verSpeed = jumpSpeed;
            }
            else
            {
                verSpeed = minFall;
            }
        }
        else
        {
            // 若已经跳起来了则将垂直方向的速度递减降低,来达到一个 下上下 的一个效果
            // Time.deltaTime 表示为每秒的刷新频率的倒数,用来控制每台电脑的移动速度都是一样的
            verSpeed += gravity * 3 * Time.deltaTime;
            // 限制最大坠落速度
            if (verSpeed < endsVelocity)
            {
                verSpeed = endsVelocity;
            }
        }
        // 给移动一个垂直速度
        movement.y = verSpeed;
        // 控制速度
        movement *= Time.deltaTime;
        // 角色控件自带的一个方法,若用 transform.Translate() 的话会无视碰撞器
        character.Move(movement);
    }
}

(4)到这里基本上的功能就都有了,这个时候添加几个粒子效果,添加了火堆、蓝白火灯、烟花。突发奇想,房子有个烟冲可以实现一个烟的效果。哈哈哈。

效果如下图:

unity虚拟现实技术场景漫游_unity_11

unity虚拟现实技术场景漫游_unity_12

    1. 到这里好像大部分的东西都有了,突发奇想,这么好的风景怎么能少了音乐呢。加上一首高中很喜欢听的钢琴曲吧-----雨中的印记。

unity虚拟现实技术场景漫游_unity_13

    1. 好像没有什么了,不知道有没有遗漏,最后就是加上一个小地图,至少得知道玩家的位置吧哈哈哈,然后在退出的时候有一个退出按钮,如下图:

unity虚拟现实技术场景漫游_unity_14

    1. 写完了突然想起来还有一个东西,那就是门的自动关闭效果。这个主要就是通过触发器和代码实现。

unity虚拟现实技术场景漫游_unity_15

代码如下:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class OpenCloseDoor : MonoBehaviour

{

    public GameObject doorLeft;

    public GameObject doorRight;


    void Start()

    {

        doorLeft = GameObject.FindGameObjectWithTag("doorLeft");

        doorRight = GameObject.FindGameObjectWithTag("doorRight");

    }


    // Update is called once per frame

    void Update()

    {

      

    }



    private void OnTriggerEnter(Collider other)

    {


        if (other.gameObject.name == "Player")

        {

            doorLeft.gameObject.transform.Rotate(Vector3.up, 90);

            doorRight.gameObject.transform.Rotate(Vector3.up, 90);

        }

    }

    private void OnTriggerExit(Collider other)

    {

        if (other.gameObject.name == "Player")

        {

            doorLeft.gameObject.transform.Rotate(Vector3.up, -90);

            doorRight.gameObject.transform.Rotate(Vector3.up, -90);

        }

    }



}

 

  • 实验小结

本次漫游项目到这里也算是完成了,也花了不少时间。总体来说效果我还是很满意的,在这个项目里面有璀璨的星空,飘动的白云,小桥流水人家,袅袅炊烟,明亮的火堆以及桥上红蓝的灯光都是特别好看的,外加迷人的烟花,让人放松的钢琴曲,整体给人一种世外桃源之美。在制作的过程我都能慢慢体会到这里面的美好,哈哈哈。不过里面还是有很多可以扩展的地方的,不过时间有限,只能暂时做到这里。里面也是有很多需要改进的地方,粒子特效也还能更进一步完善,还有声音的各种调节,碰撞检测优化以及更加精确等。总体来说,我看到最后这么一个充满美感的画面还是挺满意的,哈哈哈。差不多就到这里了。