unity跑酷游戏(源码)

包括:触发机关,

优化

fog的调试

效果

碰到障碍物游戏时间暂停(挂载到障碍物上)

上面需要有碰撞体

游戏物体上需要有标签

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Barrier : MonoBehaviour
{
    private void OnTriggerEnter(Collider other)
    {
        if (other.tag=="Player")
        {            
            Debug.Log("碰到障碍物"+ gameObject.name);
            //游戏时间流动变为0
            Time.timeScale = 0;
        }      
    }
}

结束游戏后跳出游戏结束界面

然后场景跳转

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class EndGamePoint : MonoBehaviour
{
    [SerializeField] private GameObject canvas;
    private void OnTriggerEnter(Collider other)
    {
        if(other.tag == "Player")
        {
            canvas.SetActive(true);
            SceneManager.LoadScene("Suntail Village");
        }
    }
}

升降机关

需要触发器,挂载到触发器上

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.SocialPlatforms;

public class LiftingMechanism : MonoBehaviour
{
   [SerializeField] private GameObject door;  // 要移动的对象
    private float moveDistance = 2.0f; // 门上升的距离
    private float moveSpeed = 10.0f; // 门移动的速度

    private Vector3 initialPosition;//对象的初始位置
    private bool isMoving = false;

    void Start()
    {
        if (door != null)
        {
            initialPosition = door.transform.position;
        }
        if(door==null)
        {
            Debug.LogError("没拖入升降门"); 
        }
    }

    void Update()
    {
        if (isMoving)
        {
            Vector3 targetPosition = initialPosition + new Vector3(0, moveDistance, 0);
            door.transform.position = Vector3.MoveTowards(door.transform.position, targetPosition, moveSpeed * Time.deltaTime);

            if (door.transform.position == targetPosition)
            {
                isMoving = false; // 停止移动
            }
        }
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            isMoving = true;
        }
    }
}

挂载到玩家上

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerBallRunControl : MonoBehaviour
{
    [SerializeField]private float speed =10f;
    [SerializeField] private float turnSpeed = 4f;

    void Update()
    {
        //按下R重新开始游戏
        if(Input.GetKeyDown(KeyCode.R))
        {
            //重新加载场景
            SceneManager.LoadScene(0);
            //不要重新加载音乐
            Time.timeScale = 1;
            return;
        }
        float z = Input.GetAxis("Horizontal");
        transform.Translate(speed * Time.deltaTime, 0,-z*turnSpeed*Time.deltaTime);
        //玩家落下到地面下20点的位置结束游戏
        if(transform.position.y<-20)
        {
            Time.timeScale = 0;
        }

        //屏幕动态旋转效果
        //获取主摄像机的变换组件
        var c=Camera.main.transform;
        //获得摄像机当前的旋转角度
        Quaternion cur = c.rotation;
        //将当前的旋转角度加上跟输入有关的角度(沿y轴旋转)
        Quaternion target=cur*Quaternion.Euler(0, z * 0.05f,0);
        //球形插值
        Camera.main.transform.rotation = Quaternion.Slerp(cur, target, 1.5f);

    }
}
相关推荐
KhalilRuan3 小时前
Unity-MMORPG内容笔记-其一
unity·游戏引擎
向宇it7 小时前
【unity游戏开发——网络】网络游戏通信方案——强联网游戏(Socket长连接)、 弱联网游戏(HTTP短连接)
网络·http·游戏·unity·c#·编辑器·游戏引擎
qq_1682789512 小时前
Protobuf在游戏开发中的应用:TypeScript + Golang 实践
服务器·golang·游戏引擎
ii_best13 小时前
按键精灵 安卓脚本开发:游戏实战之自动切换账号辅助工具
游戏
切韵10 天前
Unity编辑器扩展:UI绑定复制工具
ui·unity·编辑器
Alfred king10 天前
面试150跳跃游戏
python·leetcode·游戏·贪心算法
D1555408805810 天前
游戏护航小程序源码游戏派单小程序搭建游戏代练小程序源码
游戏
10 天前
Lua复习之何为闭包
开发语言·unity·游戏引擎·lua·交互
深空数字孪生11 天前
2025年小程序地图打车的5大技术革新:实时路况预测与智能调度升级
大数据·人工智能·unity·性能优化·小程序·游戏引擎
RPGMZ11 天前
RPGMZ 游戏引擎如何与lua进行互相调用 初探
开发语言·javascript·游戏引擎·lua·rpgmz