Unity实现第一人称视角迷宫游戏
        【摘要】 第一人称视角游戏
    
    
    
    制作一个简单的第一人称视角迷宫探索游戏,包含角色移动、碰撞检测、收集物品以及简单的 UI 显示功能。
一、场景搭建
1.制作场景,直接Cube搭建即可,如果有模更好,就不需要搭建了,红色的是宝石,用来收集,最下面是人物,

二、代码实现
1.角色移动脚本(PlayerMovement.cs)
代码如下:
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
    public float speed = 5f;  // 移动速度
    public float mouseSensitivity = 2f;  // 鼠标灵敏度
    private CharacterController characterController;
    private float verticalRotation = 0f;
    void Start()
    {
        characterController = GetComponent<CharacterController>();
        Cursor.lockState = CursorLockMode.Locked;  // 锁定鼠标在游戏窗口内
    }
    void Update()
    {
        // 处理角色移动
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");
        Vector3 movement = new Vector3(horizontalInput, 0f, verticalInput);
        movement = transform.TransformDirection(movement);
        movement *= speed * Time.deltaTime;
        characterController.Move(movement);
        // 处理鼠标视角控制
        float mouseX = Input.GetAxis("Mouse X");
        float mouseY = Input.GetAxis("Mouse Y");
        verticalRotation -= mouseY * mouseSensitivity;
        verticalRotation = Mathf.Clamp(verticalRotation, -89f, 89f);
        transform.localRotation = Quaternion.Euler(verticalRotation, transform.localEulerAngles.y, 0f);
        transform.Rotate(Vector3.up * mouseX * mouseSensitivity);
    }
}
2.收集品脚本(CollectibleItem.cs)
代码如下:
using UnityEngine;
using UnityEngine.UI;
public class CollectibleItem : MonoBehaviour
{
    public int scoreValue = 10;  // 收集品的分值
    private bool isCollected = false;
   
    void OnTriggerEnter(Collider other)
    {
        //记得设置人物的标签为Player
        if (other.CompareTag("Player") && !isCollected)
        {
            // 当玩家接触到收集品且未被收集时
            isCollected = true;
            AddScore(scoreValue);
            gameObject.SetActive(false);  // 隐藏收集品
        }
    }
    void AddScore(int value)
    {
        // 更新分数显示
        int currentScore = int.Parse(UIManager.instance_.scoreText.text.ToString ());
        currentScore += value;
        UIManager.instance_.scoreText.text = currentScore.ToString();
    }
}
收集品里面用的是触发器,记得把IsTrigger勾上
3.UI管理脚本(UIManager.cs)
代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIManager : MonoBehaviour
{
    public static UIManager instance_;
    // 用于显示分数的 UI 文本组件
    public Text scoreText;
    private void Awake()
    {
        instance_ = this;
    }
    // Start is called before the first frame update
    void Start()
    {
        
    }
    // Update is called once per frame
    void Update()
    {
        
    }
}
运行游戏后,玩家可以使用第一人称视角在迷宫中移动,收集放置在迷宫中的物品,分数会实时更新显示在 UI 上。
            【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
                cloudbbs@huaweicloud.com
                
            
        
        
        
        
        
        
        - 点赞
 - 收藏
 - 关注作者
 
            
           
评论(0)