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);

    }
}
相关推荐
程序猿阿伟13 小时前
《C++游戏人工智能开发:开启智能游戏新纪元》
c++·人工智能·游戏
Artistation Game15 小时前
九、怪物行为逻辑
游戏·unity·游戏引擎
百里香酚兰15 小时前
【AI学习笔记】基于Unity+DeepSeek开发的一些BUG记录&解决方案
人工智能·学习·unity·大模型·deepseek
妙为15 小时前
unreal engine5制作动作类游戏时,我们使用刀剑等武器攻击怪物或敌方单位时,发现攻击特效、伤害等没有触发
游戏·游戏引擎·虚幻·碰撞预设
网站领航15 小时前
服装时尚与动漫游戏的跨界联动:创新运营与策划策略研究
游戏·流量运营·用户运营
龙智DevSecOps解决方案15 小时前
Perforce演讲回顾(上):从UE项目Project Titan,看Helix Core在大型游戏开发中的版本控制与集成使用策略
游戏·ue5·源代码管理·perforce·helix core
dangoxiba18 小时前
[Unity Demo]从零开始制作空洞骑士Hollow Knight第十三集:制作小骑士的接触地刺复活机制以及完善地图的可交互对象
游戏·unity·visualstudio·c#·游戏引擎
新手unity自用笔记21 小时前
项目-坦克大战学习-游戏结束
学习·游戏
DK七七1 天前
【PHP陪玩系统源码】游戏陪玩系统app,陪玩小程序优势
前端·vue.js·游戏·小程序·php·uniapp
先生沉默先1 天前
使用Materialize制作unity的贴图,Materialize的简单教程,Materialize学习日志
学习·unity·贴图