Hi,
I have a mobile FPS Game where I use a joystick to rotate my Camera.
I have implemented an AimAssist but when it gets deactivated the rotation jumps back to where my last
“camRotation” was because I am never updating its value while aimLock = true;
I have tried multiple things with quaternion.euler and so on but to be honest I am fishing in the dark with this Quaternion stuff.
So my question is: How do I update my camRotation so that when I use the joystick again, the camRotation is = the current rotation? Thanks!
public class RotationClamp : MonoBehaviour
{
public float Offset;
public float aimSpeed;
public Transform target;
[SerializeField]
private Joystick joystick;
[SerializeField]
private Camera cam;
private Quaternion camRotation;
private bool aimLocked;
// Start is called before the first frame update
void Start()
{
camRotation = transform.localRotation;
_lookSpeed = lookSpeed;
}
// Update is called once per frame
void Update()
{
RaycastHit hit;
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, 100f))
{
if (hit.transform.CompareTag("Soldier"))
{
target = hit.transform;
aimLocked = true;
}
}
if (!aimLocked)
{
camRotation.x += joystick.Vertical * Time.deltaTime * _lookSpeed * verticalMultiplier * (-1);
camRotation.y += joystick.Horizontal * Time.deltaTime * _lookSpeed;
}
camRotation.x = Mathf.Clamp(camRotation.x, -verticalAngle, verticalAngle);
camRotation.y = Mathf.Clamp(camRotation.y, -horizontalAngle, horizontalAngle);
}
private void resetAimLock()
{
aimLocked = false;
target = null;
}
private void FixedUpdate()
{
if (aimLocked)
{
Quaternion OriginalRot = transform.localRotation;
transform.LookAt(target);
Quaternion NewRot = transform.localRotation;
transform.localRotation = OriginalRot;
transform.localRotation = Quaternion.Lerp(transform.localRotation, NewRot, aimSpeed * Time.deltaTime);
transform.localRotation *= Quaternion.Euler(Offset, 0, 0);
}
else
{
transform.localRotation = Quaternion.Euler(camRotation.x, camRotation.y, camRotation.z);
}
}
}