Skip to content
Snippets Groups Projects
Commit 06912a34 authored by MuhamadAjiW's avatar MuhamadAjiW
Browse files

feat: camera smoothing

parent 1b7cde03
Branches
Tags
2 merge requests!2Base arch,!1Base arch
...@@ -4,8 +4,8 @@ public class CameraFollowObject : CameraBehaviour { ...@@ -4,8 +4,8 @@ public class CameraFollowObject : CameraBehaviour {
// Attributes // Attributes
public Transform target; public Transform target;
public float followingTime = CameraConfig.DEFAULT_FOLLOWING_SPEED; public float followingTime = CameraConfig.DEFAULT_FOLLOWING_SPEED;
private Vector3 offset; protected Vector3 offset;
private Vector3 velocity; protected Vector3 velocity;
// Constructor // Constructor
protected void Start(){ protected void Start(){
...@@ -16,7 +16,6 @@ public class CameraFollowObject : CameraBehaviour { ...@@ -16,7 +16,6 @@ public class CameraFollowObject : CameraBehaviour {
protected void FixedUpdate(){ protected void FixedUpdate(){
if(GameController.instance.IsPaused) return; if(GameController.instance.IsPaused) return;
Vector3 targetPosition = target.position + offset; Vector3 targetPosition = target.position + offset;
Vector3 newPosition = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, followingTime); transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, followingTime);
transform.position = newPosition;
} }
} }
using UnityEngine; using UnityEngine;
public class CameraMouse : CameraBehaviour { public class CameraMouse : CameraFollowObject {
// Attributes // Attributes
public Transform target;
public float mouseSensitivity = 1f; public float mouseSensitivity = 1f;
public Vector2 mouseTurn; private Vector2 mouseTurn;
private Quaternion initialRotation; private Quaternion initialRotation;
private Vector3 offset; private Vector3 targetPosition;
private readonly float maximumVerticalAngle = 80f; private Quaternion targetRotation;
protected void Start(){ protected new void Start(){
offset = transform.position - target.position; base.Start();
Cursor.lockState = CursorLockMode.Locked; Cursor.lockState = CursorLockMode.Locked;
initialRotation = transform.rotation; initialRotation = transform.rotation;
} }
...@@ -22,21 +21,25 @@ public class CameraMouse : CameraBehaviour { ...@@ -22,21 +21,25 @@ public class CameraMouse : CameraBehaviour {
mouseTurn.x += Input.GetAxis("Mouse X"); mouseTurn.x += Input.GetAxis("Mouse X");
mouseTurn.y += Input.GetAxis("Mouse Y"); mouseTurn.y += Input.GetAxis("Mouse Y");
mouseTurn.y = Mathf.Clamp(mouseTurn.y, -maximumVerticalAngle, maximumVerticalAngle); mouseTurn.y = Mathf.Clamp(mouseTurn.y, -90f, 90f);
Quaternion rotation = initialRotation; Quaternion rotation = initialRotation;
rotation = Quaternion.AngleAxis(-mouseTurn.y, Vector3.right) * rotation; rotation = Quaternion.AngleAxis(-mouseTurn.y, Vector3.right) * rotation;
rotation = Quaternion.AngleAxis(mouseTurn.x, Vector3.up) * rotation; rotation = Quaternion.AngleAxis(mouseTurn.x, Vector3.up) * rotation;
Vector3 newPosition = target.position + rotation * offset;
bool hit = Physics.Linecast(target.position, newPosition, out RaycastHit hitLocation, 1); targetPosition = target.position + rotation * offset;
if (hit){ bool hit = Physics.Linecast(target.position, targetPosition, out RaycastHit hitLocation, 1);
Debug.Log("Hit Collider: " + hitLocation.collider.gameObject.name); if (hit) targetPosition = hitLocation.point;
transform.position = hitLocation.point;
} else{
transform.position = newPosition;
}
transform.localRotation = rotation; transform.localRotation = rotation;
} }
protected new void FixedUpdate(){
transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, followingTime);
// transform.localRotation = targetRotation;
return;
}
} }
...@@ -40,9 +40,9 @@ public class AttackObject : MonoBehaviour, IAttack{ ...@@ -40,9 +40,9 @@ public class AttackObject : MonoBehaviour, IAttack{
otherCollider.transform.TryGetComponent<IDamageable>(out var damageableObject); otherCollider.transform.TryGetComponent<IDamageable>(out var damageableObject);
if(damageableObject == null) return true; if(damageableObject == null) return true;
Debug.Log(string.Format("Hit in hitbox of {0} by {1} with damage of {2}", transform.name, otherCollider.transform.name, Damage));
if(damageableObject.Damageable){ if(damageableObject.Damageable){
Debug.Log(string.Format("Hit in hitbox of {0} by {1} with damage of {2}", transform.name, otherCollider.transform.name, Damage));
damageableObject.InflictDamage(Damage); damageableObject.InflictDamage(Damage);
OnDamageEvent?.Invoke(); OnDamageEvent?.Invoke();
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment