- Home /
Camera/can't turn on X
Camera is rotating on Y but not on X;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraHandler : MonoBehaviour
{
public Transform camTrans;
public Transform pivot;
public Transform character;
public Transform mTransform;
public CharacterStatus characterStatus;
public CameraConfig cameraConfig;
public bool leftPivot;
public float delta;
public float mouseX;
public float mouseY;
public float smoothX;
public float smoothY;
public float smoothXVelocity;
public float smoothYVelocity;
public float lookAngle;
public float tiltAngle;
private void FixedUpdate()
{
FixedTick();
}
void FixedTick()
{
delta = Time.deltaTime;
HandlePosition();
HandleRotation();
Vector3 targetPosition = Vector3.Lerp(mTransform.position, character.position, 1);
mTransform.position = targetPosition;
}
void HandlePosition()
{
float targetX = cameraConfig.normalX;
float targetY = cameraConfig.normalY;
float targetZ = cameraConfig.normalZ;
if (characterStatus.isAiming)
{
targetX = cameraConfig.aimX;
targetZ = cameraConfig.aimZ;
}
if (leftPivot)
{
targetX = -targetX;
}
Vector3 newPivotPosition = pivot.localPosition;
newPivotPosition.x = targetX;
newPivotPosition.y = targetY;
Vector3 newCameraPosition = camTrans.localPosition;
newCameraPosition.z = targetZ;
float t = delta * cameraConfig.pivotSpeed;
pivot.localPosition = Vector3.Lerp(pivot.localPosition, newPivotPosition,t);
camTrans.localPosition = Vector3.Lerp(camTrans.localPosition, newCameraPosition, t);
}
void HandleRotation()
{
mouseX = Input.GetAxis("Mouse X");
mouseY = Input.GetAxis("Mouse Y");
if(cameraConfig.turnSmooth > 0)
{
smoothX = Mathf.SmoothDamp(smoothX,mouseX,ref smoothXVelocity,cameraConfig.turnSmooth);
smoothY = Mathf.SmoothDamp(smoothY, mouseY, ref smoothYVelocity, cameraConfig.turnSmooth);
}
else
{
smoothX = mouseX;
smoothY = mouseY;
}
lookAngle += smoothX * cameraConfig.Y_Rotation_Speed;
Quaternion targetRot = Quaternion.Euler(0, lookAngle, 0);
tiltAngle -= smoothY * cameraConfig.Y_Rotation_Speed;
tiltAngle = Mathf.Clamp(tiltAngle, cameraConfig.minAngle, cameraConfig.maxAngle);
pivot.localRotation = Quaternion.Euler(tiltAngle,0,0);
}
}
Character status and Camera config are simple scriptable objects with no functions;
Please be respectful of people's time if they wish to help you. Simply posting your code and saying it doesn't work is not exactly making it easy for someone to help you. Try to show us what you have tried before. Boil it down to a very simple reproducible aspect of the code, which will normally help you fix the problem yourself, and then ask for help. I cannot simply copy paste this code into my own project to help you as it is reliant on all sorts of different things, meaning it would take me a while to figure this out. Keep that in $$anonymous$$d when posting.
refer to these posts:
How do I post a good question - Unity
How do I post a good question - Stackoverflow
@SteenPetersen Okay, won't happen again. I think that problem lies in HandleRotation() but I really can't see where is the problem, so I don't know what to try either
Answer by conguerror · May 30, 2020 at 08:40 AM
Solved it:
lookAngle += smoothX * cameraConfig.Y_Rotation_Speed;
Quaternion targetRot = Quaternion.Euler(0, lookAngle, 0);
mTransform.rotation = targetRot;
tiltAngle -= smoothY * cameraConfig.Y_Rotation_Speed;
tiltAngle = Mathf.Clamp(tiltAngle, cameraConfig.minAngle, cameraConfig.maxAngle);
pivot.localRotation = Quaternion.Euler(tiltAngle,0,0);