OptimizedCode in Camera Controller

This commit is contained in:
Thomas Hodnemyr 2022-09-18 00:39:26 +02:00
parent 2275195932
commit fb6a6bb198
1 changed files with 6 additions and 3 deletions

View File

@ -9,6 +9,7 @@ namespace AsteroidGame
private CameraControllActions cameraActions; private CameraControllActions cameraActions;
private InputAction movement; private InputAction movement;
private Transform cameraTransform; private Transform cameraTransform;
private Camera cameraReference;
// Horizontal Movement // Horizontal Movement
[SerializeField] [SerializeField]
@ -53,7 +54,9 @@ namespace AsteroidGame
private void Awake() private void Awake()
{ {
cameraActions = new CameraControllActions(); cameraActions = new CameraControllActions();
cameraTransform = this.GetComponentInChildren<Camera>().transform;
cameraReference = this.GetComponentInChildren<Camera>();
cameraTransform = cameraReference.transform;
} }
private void OnEnable() private void OnEnable()
@ -130,7 +133,7 @@ namespace AsteroidGame
if (targetPosition.sqrMagnitude > 0.1f) if (targetPosition.sqrMagnitude > 0.1f)
{ {
speed = Mathf.Lerp(speed, maxSpeed, Time.deltaTime * acceleration); speed = Mathf.Lerp(speed, maxSpeed, Time.deltaTime * acceleration);
transform.position += targetPosition * speed * Time.deltaTime; transform.position += speed * Time.deltaTime * targetPosition;
} }
else else
{ {
@ -195,7 +198,7 @@ namespace AsteroidGame
if (!Mouse.current.rightButton.isPressed) { return; } if (!Mouse.current.rightButton.isPressed) { return; }
Ray ray = Camera.main.ScreenPointToRay(Mouse.current.position.ReadValue()); Ray ray = cameraReference.ScreenPointToRay(Mouse.current.position.ReadValue());
if (cameraPlane.Raycast(ray,out float distance)) if (cameraPlane.Raycast(ray,out float distance))
{ {
if (Mouse.current.rightButton.wasPressedThisFrame) if (Mouse.current.rightButton.wasPressedThisFrame)