欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 美景 > unity跑酷游戏(源码)

unity跑酷游戏(源码)

2024/11/30 8:51:49 来源:https://blog.csdn.net/m0_74289471/article/details/139702078  浏览:    关键词:unity跑酷游戏(源码)

包括:触发机关,

 优化

fog的调试

效果

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

上面需要有碰撞体

游戏物体上需要有标签

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);//游戏时间流动变为0Time.timeScale = 0;}      }
}

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

然后场景跳转

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

升降机关

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

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

挂载到玩家上

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com