• 【UGUI】 (二) 小地图


    在绝大多数游戏中,小地图都是极为常见的一个模块而且十分重要。在Unity里面如何制作一个地图其实也是比较简单的

     一. 创建玩家与敌人

    创建一个Capsule,命名为Player,代表我们的游戏玩家,创建两个Cube,分别命名为EnemyA,EnemyB,代表敌方两个NPC

    二. 创建摄像机

    创建摄像机,命名为Mini Cam,并置于模型上方适当位置处,X轴旋转90°产生俯视视觉效果,Project调整为Orthographic,Size根据各人具体情况来进行调整

     三. 创建UI显示效果

    ①在 Assets -> Create -> RenderTexture 创建一个RenderTexture,命名为MiniMap,并将其赋值给第二步创建的Mini Cam的Target Texture。

    ②创建一个Raw Image,并将MiniMap赋值给RawImage的Texture,并把RawImage置于屏幕右上角,就可以看到初步效果了

    ③为了更明显的显示效果,创建两个Material,一个调成蓝色,赋值给Player,另一个调成红色赋值给两个Enemy。然后给Player和两个Enemy的Layer设置成MiniMap。再把Mini Camde 的Culling Mask调整成只显示MiniMap层

    效果如下

    四. 让角色随机移动,观察效果

    创建一个C#脚本,并给角色和敌人都加上此脚本和刚体。运行,观察效果。

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class RandomMove : MonoBehaviour {
    
    	private Vector3 velocity = Vector3.zero;
    	private float speed = 8;
    	private Vector3 target = Vector3.zero;
    	private void Update()
    	{
    
    		if (Random.value < 0.01f)
    			target = transform.position + Quaternion.Euler(0, Random.value * 360, 0) * Vector3.right * 10;
    		Vector3 direct = target - transform.position;
    		direct.y = 0;												//防止y方向移动
    		if (direct.sqrMagnitude > 1)
    		{
    			transform.rotation = Quaternion.LookRotation(direct);	//改变朝向
    			velocity = direct.normalized * speed / 3;
    		}
    
    		velocity -= transform.GetComponent<Rigidbody>().velocity;
    		velocity.y = 0;
    
    		//速度过大时减速
    		if (velocity.sqrMagnitude > speed * speed)
    			velocity = velocity.normalized * speed;
    
    		transform.GetComponent<Rigidbody>().AddForce(velocity, ForceMode.VelocityChange);
    		velocity = Vector3.zero;
    	}
    }

    五. 总结

    其实制作小地图的整个流程并不复杂,原理上和放大镜都是应用了RenderTexture和摄像机的渲染,只要了解Unity中摄像机渲染的相关事项,整个操作过程就会变得十分简单明了。有兴趣的朋友,可以给小地图多加一张Mask,让其更像一个小地图。

  • 相关阅读:
    CentOS7.2上安装Python3.6
    Ubuntu 18.04 安装配置 MySQL 5.7
    CentOS 使用 sudo 遇到 command not found 问题解决
    一个恼人的文件名问题
    CentOS7 安装配置备忘录
    Win7 + CentOS7 双系统
    C++ 精英化趋势
    CentOS7 安装配置 MySQL 5.7
    pip "Cannot uninstall 'six'. It is a distutils installed project..." 解决方法
    Linux 系统假死的解决方案
  • 原文地址:https://www.cnblogs.com/BFXYMY/p/9683634.html
Copyright © 2020-2023  润新知