case5
This commit is contained in:
8
Assets/Scripts/StudyCase5.meta
Normal file
8
Assets/Scripts/StudyCase5.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a5af99134a9e2b24ea8e52bb2d3728c5
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
73
Assets/Scripts/StudyCase5/NetWorkPlayer.cs
Normal file
73
Assets/Scripts/StudyCase5/NetWorkPlayer.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
using UnityEngine;
|
||||
using Mirror;
|
||||
using UnityEngine.InputSystem;
|
||||
using Cinemachine;
|
||||
|
||||
public class NetWorkPlayer : NetworkBehaviour
|
||||
{
|
||||
public CharacterController characterController;
|
||||
public InputActionAsset inputAction;
|
||||
public Animator animator;
|
||||
public Transform forward;
|
||||
public Transform model;
|
||||
public CinemachineVirtualCamera vCam;
|
||||
public CursorLockMode cursorLock;
|
||||
public float moveSpeed = 5f;
|
||||
public float jumpSpeed = 2f;
|
||||
public float turnSpeed = 10f;
|
||||
public float gravity = 10f;
|
||||
Vector3 moveDir;
|
||||
Vector2 moveInput;
|
||||
|
||||
public override void OnStartLocalPlayer()
|
||||
{
|
||||
Cursor.lockState = cursorLock;
|
||||
vCam.Priority = 15;
|
||||
inputAction.FindAction("Move").started += OnMove;
|
||||
inputAction.FindAction("Move").performed += OnMove;
|
||||
inputAction.FindAction("Move").canceled += OnMove;
|
||||
inputAction.FindAction("Jump").performed += OnJump;
|
||||
inputAction.FindAction("Esc").performed += OnEsc;
|
||||
inputAction.Enable();
|
||||
}
|
||||
private void Update()
|
||||
{
|
||||
if (!isLocalPlayer) return;
|
||||
moveDir = new Vector3(moveInput.x, moveDir.y, moveInput.y);
|
||||
forward.eulerAngles = new Vector3(0, vCam.transform.eulerAngles.y, 0);
|
||||
moveDir = forward.TransformDirection(moveDir);
|
||||
if (moveInput != Vector2.zero)
|
||||
{
|
||||
animator.SetBool("Move", true);
|
||||
Quaternion target = Quaternion.LookRotation(new Vector3(moveDir.x, 0, moveDir.z));
|
||||
model.rotation = Quaternion.Slerp(model.rotation, target, turnSpeed * Time.deltaTime);
|
||||
}
|
||||
else animator.SetBool("Move", false);
|
||||
if (!characterController.isGrounded)
|
||||
moveDir.y -= gravity * Time.deltaTime;
|
||||
characterController.Move(moveDir * moveSpeed * Time.deltaTime);
|
||||
}
|
||||
public void OnMove(InputAction.CallbackContext context)
|
||||
{
|
||||
if (!isLocalPlayer) return;
|
||||
if (characterController.isGrounded)
|
||||
moveInput = context.ReadValue<Vector2>();
|
||||
else moveInput = Vector2.zero;
|
||||
}
|
||||
public void OnJump(InputAction.CallbackContext context)
|
||||
{
|
||||
if (!isLocalPlayer) return;
|
||||
if (characterController.isGrounded)
|
||||
{
|
||||
moveDir.y = jumpSpeed;
|
||||
}
|
||||
}
|
||||
public void OnEsc(InputAction.CallbackContext context)
|
||||
{
|
||||
if (!isLocalPlayer) return;
|
||||
if(Cursor.lockState == CursorLockMode.Locked)
|
||||
Cursor.lockState = CursorLockMode.None;
|
||||
else
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/StudyCase5/NetWorkPlayer.cs.meta
Normal file
11
Assets/Scripts/StudyCase5/NetWorkPlayer.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5a50a24b97ece1f4aa01436e96582c5a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user