mobile wallpaper 1mobile wallpaper 2mobile wallpaper 3mobile wallpaper 4
287 字
1 分钟
unity3D游戏开发笔记(一)
2026-03-03

关于 ClianWorld 测试版使用脚本记录(一) :#

描述:#

C联致力于开发的开放世界虚拟基地,基于unity游戏引擎,按照《次元永生》界海世界观进行打造。

同时,我们将继续进行游戏性的开发,探索更多的游戏模式以及可玩性,最终打造划时代的产品。

这里记录一些之前用到的脚本

记录:#

1.基于Character Contorller的角色移动脚本(基础版)#

功能:实现游戏角色的前后移动和方向旋转

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private CharacterController controller;
public float Speed = 10f;
public float RotateSpeed = 1f;
public float Gravity = -9.8f;
private Vector3 Velocity = Vector3.zero;
public Transform GroundCheck;
public float CheckRadius = 0.2f;
private bool IsGround;
public LayerMask layerMask;
public float JumpHeight = 3f;
void Start()
{
controller = transform.GetComponent<CharacterController>();
}
void Update()
{
MoveLikeWow();
}
private void MoveLikeWow()
{
IsGround = Physics.CheckSphere(GroundCheck.position, CheckRadius, layerMask);
if(IsGround && Velocity.y<0)
{
Velocity.y = 0;
}
var horizontal = Input.GetAxis("Horizontal");
var vertical = Input.GetAxis("Vertical");
var move = transform.forward * Speed * vertical * Time.deltaTime;
controller.Move(move);
Velocity.y += Gravity + Time.deltaTime;
controller.Move(Velocity * Time.deltaTime);
transform.Rotate(Vector3.up, horizontal * RotateSpeed);
}
}

2.全局场景跳转脚本(帮助界面为例)#

功能:由一个场景(Scene)跳转到其他场景

using UnityEngine;
using UnityEngine.SceneManagement;
public class Help : MonoBehaviour
{
// 菜单界面的场景名称
public string menuSceneName = "help";
void Update()
{
// 检测是否按下H键
if (Input.GetKeyDown(KeyCode.H))
{
// 加载帮助菜单场景
SceneManager.LoadScene("help");
}
}
}

3.绑定按钮场景跳转脚本(开始菜单为例)#

功能:通过绑定到按钮上,实现场景跳转

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class FirstWorld : MonoBehaviour
{
public void LoadGame()
{
SceneManager.LoadScene("firstworld");
}
}

4.隐藏鼠标#

(修改参数可以控制鼠标指针是否显示)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class killmouse : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Cursor.visible = false; // 隐藏鼠标
}
}

5.退出游戏(EXIT)#

功能:绑定按钮,实现点击退出游戏

using UnityEngine;
public class ExitGameButton : MonoBehaviour
{
public void OnExitButtonClick()
{
// 调用Application类的方法来退出游戏
Application.Quit();
}
}

6.第三人称自由视角(镜头)#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class ThirdPersonCamera : MonoBehaviour
{
private GameObject _mainCamera;
[Header("Cinemachine")]
[Tooltip("跟随的目标")]
public GameObject CameraTarget;
[Tooltip("上移动的最大角度")]
public float TopClamp = 70.0f;
[Tooltip("下移动的最大角度")]
public float BottomClamp = -30.0f;
private const float _threshold = 0.01f;
private float _cinemachineTargetYaw;
private float _cinemachineTargetPitch;
private void Start()
{
if (_mainCamera == null)
{
_mainCamera = GameObject.FindGameObjectWithTag("MainCamera");
}
_cinemachineTargetYaw = CameraTarget.transform.rotation.eulerAngles.y;
}
private void Update()
{
if (_look.sqrMagnitude >= _threshold)
{
_cinemachineTargetYaw += _look.x;
_cinemachineTargetPitch -= _look.y;
}
_cinemachineTargetYaw = ClampAngle(_cinemachineTargetYaw, float.MinValue, float.MaxValue);
_cinemachineTargetPitch = ClampAngle(_cinemachineTargetPitch, BottomClamp, TopClamp);
CameraTarget.transform.rotation = Quaternion.Euler(_cinemachineTargetPitch, _cinemachineTargetYaw, 0.0f);
}
private static float ClampAngle(float lfAngle, float lfMin, float lfMax)
{
if (lfAngle < -360f) lfAngle += 360f;
if (lfAngle > 360f) lfAngle -= 360f;
return Mathf.Clamp(lfAngle, lfMin, lfMax);
}
private Vector2 _look;
public void OnLook(InputValue value)
{
_look = value.Get<Vector2>();
}
}
分享

如果这篇文章对你有帮助,欢迎分享给更多人!

unity3D游戏开发笔记(一)
http://czxh.top/posts/unity3d开发笔记/
作者
Mitunlny & Pr0mis3
发布于
2026-03-03
许可协议
CC BY-NC-SA 4.0

部分信息可能已经过时

目录