Move through camera direction. Simple x-ray shader for player
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
using FishNet.Object;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Players
|
||||
{
|
||||
[RequireComponent(typeof(Camera))]
|
||||
public sealed class CameraFollow : NetworkBehaviour
|
||||
{
|
||||
[SerializeField] private Vector3 _offset = new(-10, 15, -10);
|
||||
[SerializeField] private Vector3 _rotation = new(45, 45, 0);
|
||||
[SerializeField] private Transform _target;
|
||||
[SerializeField, Range(0f, 360f)] private float _orbitAngle;
|
||||
[SerializeField] private float _mouseOrbitSensitivity = 180f;
|
||||
|
||||
private float _mouseOrbitAngle;
|
||||
|
||||
public override void OnStartClient()
|
||||
{
|
||||
base.OnStartClient();
|
||||
|
||||
if(!IsOwner)
|
||||
return;
|
||||
|
||||
Camera cam = GetComponent<Camera>();
|
||||
cam.enabled = true;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if(!IsOwner || _target == null)
|
||||
return;
|
||||
|
||||
if (Input.GetMouseButton(1))
|
||||
_mouseOrbitAngle += Input.GetAxis("Mouse X") * _mouseOrbitSensitivity * Time.deltaTime;
|
||||
|
||||
var orbitRotation = Quaternion.Euler(0f, _orbitAngle + _mouseOrbitAngle, 0f);
|
||||
|
||||
transform.position = _target.position + orbitRotation * _offset;
|
||||
transform.rotation = orbitRotation * Quaternion.Euler(_rotation);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9cd37f3ec0bf4c66960bab1545ac666f
|
||||
timeCreated: 1775572726
|
||||
@@ -1,4 +1,3 @@
|
||||
using System;
|
||||
using FishNet.Object;
|
||||
using UnityEngine;
|
||||
|
||||
@@ -8,6 +7,15 @@ namespace Players
|
||||
{
|
||||
[SerializeField] private float _moveSpeed = 5f;
|
||||
[SerializeField] private CharacterController _characterController;
|
||||
|
||||
private Transform _cameraTransform;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
Camera playerCamera = GetComponentInChildren<Camera>(true);
|
||||
if (playerCamera != null)
|
||||
_cameraTransform = playerCamera.transform;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
@@ -16,10 +24,14 @@ namespace Players
|
||||
|
||||
float horizontal = Input.GetAxisRaw("Horizontal");
|
||||
float vertical = Input.GetAxisRaw("Vertical");
|
||||
|
||||
Vector3 offset = new Vector3(horizontal, Physics.gravity.y, vertical).normalized * (_moveSpeed * Time.deltaTime);
|
||||
|
||||
Transform directionSource = _cameraTransform != null ? _cameraTransform : transform;
|
||||
Vector3 forward = Vector3.ProjectOnPlane(directionSource.forward, Vector3.up).normalized;
|
||||
Vector3 right = Vector3.ProjectOnPlane(directionSource.right, Vector3.up).normalized;
|
||||
Vector3 moveDirection = (right * horizontal + forward * vertical).normalized;
|
||||
Vector3 offset = moveDirection * (_moveSpeed * Time.deltaTime);
|
||||
|
||||
_characterController.Move(offset);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
using FishNet.Object;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Players
|
||||
{
|
||||
public sealed class PlayerRevealController : NetworkBehaviour
|
||||
{
|
||||
private static readonly int RevealCenterId = Shader.PropertyToID("_RevealCenter");
|
||||
private static readonly int RevealRadiusId = Shader.PropertyToID("_GlobalRevealRadius");
|
||||
private static readonly int RevealEdgeSoftnessId = Shader.PropertyToID("_GlobalRevealEdgeSoftness");
|
||||
|
||||
[SerializeField] private Transform _center;
|
||||
[SerializeField, Min(0f)] private float _revealRadius = 6f;
|
||||
[SerializeField, Min(0.001f)] private float _edgeSoftness = 1f;
|
||||
[SerializeField] private Vector3 _centerOffset;
|
||||
|
||||
public override void OnStartClient()
|
||||
{
|
||||
base.OnStartClient();
|
||||
UpdateShaderGlobals();
|
||||
}
|
||||
|
||||
public override void OnStopClient()
|
||||
{
|
||||
if (IsOwner)
|
||||
ClearShaderGlobals();
|
||||
|
||||
base.OnStopClient();
|
||||
}
|
||||
|
||||
private void LateUpdate()
|
||||
{
|
||||
if (!IsOwner)
|
||||
return;
|
||||
|
||||
UpdateShaderGlobals();
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
if (IsOwner)
|
||||
ClearShaderGlobals();
|
||||
}
|
||||
|
||||
private void UpdateShaderGlobals()
|
||||
{
|
||||
if (!IsOwner)
|
||||
return;
|
||||
|
||||
Transform center = _center != null ? _center : transform;
|
||||
Vector3 worldCenter = center.position + _centerOffset;
|
||||
|
||||
Shader.SetGlobalVector(RevealCenterId, new Vector4(worldCenter.x, worldCenter.y, worldCenter.z, 1f));
|
||||
Shader.SetGlobalFloat(RevealRadiusId, _revealRadius);
|
||||
Shader.SetGlobalFloat(RevealEdgeSoftnessId, _edgeSoftness);
|
||||
}
|
||||
|
||||
private void ClearShaderGlobals()
|
||||
{
|
||||
Shader.SetGlobalVector(RevealCenterId, Vector4.zero);
|
||||
Shader.SetGlobalFloat(RevealRadiusId, 0f);
|
||||
Shader.SetGlobalFloat(RevealEdgeSoftnessId, 0f);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9da7d22d0c6d4292ab5d5c0c93ed65af
|
||||
timeCreated: 1775565000
|
||||
Reference in New Issue
Block a user