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

描述:

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

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

记录:

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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)跳转到其他场景

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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.绑定按钮场景跳转脚本(开始菜单为例)

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

1
2
3
4
5
6
7
8
9
10
11
12
13
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class FirstWorld : MonoBehaviour
{
public void LoadGame()
{
SceneManager.LoadScene("firstworld");
}
}

4.隐藏鼠标

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

1
2
3
4
5
6
7
8
9
10
11
12
13
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)

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

1
2
3
4
5
6
7
8
9
using UnityEngine;
public class ExitGameButton : MonoBehaviour
{
public void OnExitButtonClick()
{
// 调用Application类的方法来退出游戏
Application.Quit();
}
}

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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>();
}
}