1
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace NGS.AdvancedCullingSystem.Tutorial
|
||||
{
|
||||
public class CameraController : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
private float mouseSensitivity = 5f;
|
||||
|
||||
[SerializeField]
|
||||
private float movementSpeed = 5f;
|
||||
|
||||
[SerializeField]
|
||||
private float _height = 5f;
|
||||
|
||||
private float _xRotation = 0f;
|
||||
private float _currentHeight;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
|
||||
_currentHeight = transform.parent.position.y;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * 100 * Time.deltaTime;
|
||||
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * 100 * Time.deltaTime;
|
||||
|
||||
mouseX = Mathf.Clamp(mouseX, -10, 10);
|
||||
mouseY = Mathf.Clamp(mouseY, -10, 10);
|
||||
|
||||
Rotate(mouseX, mouseY);
|
||||
|
||||
float horizontal = Input.GetAxis("Horizontal");
|
||||
float vertical = Input.GetAxis("Vertical");
|
||||
|
||||
Vector3 movement = CalculateMovement(horizontal, vertical);
|
||||
Vector3 position = transform.parent.position;
|
||||
|
||||
_currentHeight = Mathf.Lerp(_currentHeight, GetHeight(), movementSpeed * Time.deltaTime);
|
||||
|
||||
position += movement * movementSpeed * Time.deltaTime;
|
||||
position.y = _currentHeight;
|
||||
|
||||
MoveToPoint(position);
|
||||
}
|
||||
|
||||
|
||||
private void Rotate(float mouseX, float mouseY)
|
||||
{
|
||||
_xRotation -= mouseY;
|
||||
_xRotation = Mathf.Clamp(_xRotation, -90f, 90f);
|
||||
|
||||
transform.localRotation = Quaternion.Euler(_xRotation, 0f, 0f);
|
||||
transform.parent.Rotate(Vector3.up * mouseX);
|
||||
}
|
||||
|
||||
private Vector3 CalculateMovement(float horizontal, float vertical)
|
||||
{
|
||||
Vector3 movement = transform.right * horizontal + transform.forward * vertical;
|
||||
|
||||
movement.y = 0f;
|
||||
|
||||
return movement.normalized;
|
||||
}
|
||||
|
||||
private void MoveToPoint(Vector3 targetPoint)
|
||||
{
|
||||
Vector3 start = transform.parent.position;
|
||||
start.y = targetPoint.y;
|
||||
|
||||
Ray ray = new Ray(start, targetPoint - start);
|
||||
|
||||
if (Physics.Raycast(ray, out RaycastHit hit, 3f))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
transform.parent.position = targetPoint;
|
||||
}
|
||||
|
||||
private float GetHeight()
|
||||
{
|
||||
Ray ray = new Ray(transform.parent.position + Vector3.up * 20, -transform.parent.up);
|
||||
|
||||
if (Physics.Raycast(ray, out RaycastHit hit))
|
||||
{
|
||||
return hit.point.y + _height;
|
||||
}
|
||||
|
||||
return _height;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,31 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace NGS.AdvancedCullingSystem.Tutorial
|
||||
{
|
||||
public class DisabledRendererGizmo : MonoBehaviour
|
||||
{
|
||||
private Renderer _target;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_target = GetComponent<Renderer>();
|
||||
|
||||
if (_target == null)
|
||||
enabled = false;
|
||||
}
|
||||
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
if (_target == null)
|
||||
return;
|
||||
|
||||
if (_target.enabled)
|
||||
return;
|
||||
|
||||
Gizmos.color = Color.red;
|
||||
Gizmos.DrawWireCube(_target.bounds.center, _target.bounds.size);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,33 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering;
|
||||
using NGS.AdvancedCullingSystem.Static;
|
||||
|
||||
namespace NGS.AdvancedCullingSystem.Tutorial
|
||||
{
|
||||
public class DisabledTargetGizmo : MonoBehaviour
|
||||
{
|
||||
private CullingTarget _target;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_target = GetComponent<CullingTarget>();
|
||||
|
||||
if (_target == null)
|
||||
enabled = false;
|
||||
}
|
||||
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
if (_target == null)
|
||||
return;
|
||||
|
||||
if (_target.enabled)
|
||||
return;
|
||||
|
||||
Gizmos.color = Color.red;
|
||||
Gizmos.DrawWireCube(_target.Bounds.center, _target.Bounds.size);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,47 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace NGS.AdvancedCullingSystem.Tutorial
|
||||
{
|
||||
public class Spawner : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
private GameObject _prefab;
|
||||
|
||||
[SerializeField]
|
||||
private int _maxInstances = 2000;
|
||||
|
||||
[SerializeField]
|
||||
private float _delay = 0.02f;
|
||||
|
||||
private int _counter;
|
||||
private float _timer;
|
||||
|
||||
|
||||
private void Update()
|
||||
{
|
||||
_timer += Time.deltaTime;
|
||||
|
||||
if (_timer > _delay)
|
||||
{
|
||||
Spawn();
|
||||
|
||||
_timer = 0;
|
||||
_counter++;
|
||||
|
||||
if (_counter > _maxInstances)
|
||||
enabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void Spawn()
|
||||
{
|
||||
GameObject instance = Instantiate(_prefab);
|
||||
|
||||
instance.transform.position = transform.position;
|
||||
instance.transform.rotation = Random.rotation;
|
||||
instance.transform.localScale = Vector3.one * Random.Range(1, 2.5f);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user