- Home /
Need help with storing Rotation in a Quaternion
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);
}
}
}
Answer by UnityedWeStand · Jul 10, 2020 at 09:22 PM
Quick Solution
whenever you switch off aim assist, just convert the transform's current rotation into euler angles.
camRotation = transform.rotation.eulerAngles
Recommended Solution
There is almost no reason to separately declare an XYZ rotation
camRotation
variable to store and modify the camera's rotation. Quaternions are so much easier to work with once you get the hang of them. For example, I would rewrite lines 43 and 44 to be
if (!aimLocked)
{
transform.rotation = Quaternion.AngleAxis(joystick.Vertical * Time.deltaTime * _lookSpeed * verticalMultiplier * (-1), transform.right) * transform.rotation;
transform.rotation = Quaternion.AngleAxis(joystick.Horizontal * Time.deltaTime * _lookSpeed, transform.up) * transform.rotation;
}
and then you don't have to deal with a separate camRotation
variable and convert back and forth between euler rotations and Quaternions.
Yes, that works pretty good but now I also got a z rotation. How do I lock that axis?
Also: How would you clamp the rotation?
Try adding this after you set camRotation
camRotation.x = $$anonymous$$athf.Clamp(camRotation.x, -verticalAngle, verticalAngle);
camRotation.y = $$anonymous$$athf.Clamp(camRotation.y, -horizontalAngle, horizontalAngle);
camRotation.z = 0;
Again, I highly recommend using Quaternions directly, since converting back and forth from euler angles sometimes doesn't behave nicely
Ok but that would require me to again use the camRotation variable. Is there a way to clamp the rotation with the way you have shown in your first answer?