Created
August 18, 2019 13:26
-
-
Save 314pies/3f456cf5e34d72fff8394ddab869bfa5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using LW.Gameplay.Player; | |
namespace LW.Gameplay.Weapons | |
{ | |
public class Aimer : MonoBehaviour | |
{ | |
[ReadOnly] | |
public FirstPersonWeapon weapon; | |
[Tooltip("The game object to take aiming effect")] | |
[SerializeField] | |
private Transform AimingObject; | |
[Tooltip("How long to take to aim")] | |
[SerializeField] | |
private float delay = 0.15f; | |
/// <summary> | |
/// How long to take to aim | |
/// </summary> | |
public float Delay | |
{ | |
get { return delay; } | |
set | |
{ | |
if (value < 0.05) { delay = 0.05f; return; } | |
delay = value; | |
} | |
} | |
/// <summary> | |
/// The aiming position | |
/// </summary> | |
[SerializeField] | |
public Vector3 aimPos; | |
[SerializeField] | |
public Vector3 aimRot; | |
/// <summary> | |
/// The original position | |
/// </summary> | |
private Vector3 OrinPos; | |
private Vector3 OrinRot; | |
/// <summary> | |
///Euler angle will turn negative number to positive, | |
///so it will need an extra field for storing lat euler angle | |
///ex: Euler angle will become 355 if you set -5 to it | |
/// </summary> | |
private Vector3 lastEulerAngle; | |
private float nextField = 60.0f; | |
private Vector3 dampPos, dampRot; | |
private float dampF = 0.3f; | |
[SerializeField] | |
private float aimField = 20; | |
public float AimField | |
{ | |
get { return aimField; } | |
set | |
{ | |
if (value < 5) | |
{ | |
aimField = 5; | |
return; | |
} | |
aimField = value; | |
} | |
} | |
private float OrinField = 60; | |
/// <summary> | |
/// The camera to take aiming effect | |
/// </summary> | |
[ReadOnly] | |
public Camera Cam; | |
[Tooltip("Leave it empty if there's no lens(ex: iron sight)")] | |
public GameObject Lens = null; | |
bool isInitialize=false; | |
public void SetupAimer(FirstPersonWeapon weapon,Camera firstPersonCam) | |
{ | |
OrinPos = AimingObject.transform.localPosition; | |
OrinRot = AimingObject.transform.localEulerAngles; | |
Cam = firstPersonCam; | |
OrinField = Cam.fieldOfView; | |
this.weapon = weapon; | |
isInitialize = true; | |
} | |
void Update() | |
{ | |
if (!isInitialize) return; | |
//Smoothly update position and view | |
NextPos = Vector3.SmoothDamp(AimingObject.localPosition, NextPos, ref dampPos, delay); | |
NextRot = Vector3.SmoothDamp(lastEulerAngle, NextRot, ref dampRot, delay); | |
float newField = Mathf.SmoothDamp(Cam.fieldOfView, nextField, ref dampF, delay); | |
AimingObject.localPosition = NextPos; | |
lastEulerAngle = NextRot; | |
AimingObject.localEulerAngles = lastEulerAngle; | |
Cam.fieldOfView = newField; | |
if (weapon.IsAiming) | |
{ | |
NextPos = aimPos; | |
NextRot = aimRot; | |
nextField = aimField; | |
if (Lens != null && !Lens.activeSelf) | |
Lens.SetActive(true); | |
} | |
else | |
{ | |
NextPos = OrinPos; | |
NextRot = OrinRot; | |
nextField = OrinField; | |
if (Lens != null && Lens.activeSelf) | |
Lens.SetActive(false); | |
} | |
} | |
[Header("Status")] | |
[ShownAsLabel] | |
[SerializeField] | |
private Vector3 NextPos; | |
[ShownAsLabel] | |
[SerializeField] | |
private Vector3 NextRot; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment