Hi Dears,
I found following script for move my camera in game.
Main Character (player) rotates according to the rotation of the camera.
But when I press “W” or “Up Arrow” and character move forward, camera is locked and i can move only in up/down, not in right/left.
Could you tell me why? I trying modify this script but I cant find this moment which block it.
Thanks for any help
void Start()
{
Vector3 Angles = transform.eulerAngles;
x = Angles.x;
y = Angles.y;
currentDistance = distance;
desireDistance = distance;
correctedDistance = distance;
}
void LateUpdate()
{
x += Input.GetAxis("Mouse X") * mouseXSpeedMod;
y += Input.GetAxis("Mouse Y") * mouseYSpeedMod;
if (Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0)
{
float targetRotantionAngle = CameraTarget.eulerAngles.y;
float cameraRotationAngle = transform.eulerAngles.y;
x = Mathf.LerpAngle(cameraRotationAngle, targetRotantionAngle, lerpRate * Time.deltaTime);
}
y = ClampAngle(y, -15, 25);
Quaternion rotation = Quaternion.Euler(y, x, 0);
desireDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * ZoomRate * Mathf.Abs(desireDistance);
desireDistance = Mathf.Clamp(desireDistance, MinViewDistance, MaxViewDistance);
correctedDistance = desireDistance;
Vector3 position = CameraTarget.position - (rotation * Vector3.forward * desireDistance);
RaycastHit collisionHit;
Vector3 cameraTargetPosition = new Vector3(CameraTarget.position.x, CameraTarget.position.y + cameraTargetHeight, CameraTarget.position.z);
bool isCorrected = false;
if (Physics.Linecast(cameraTargetPosition, position, out collisionHit))
{
position = collisionHit.point;
correctedDistance = Vector3.Distance(cameraTargetPosition, position);
isCorrected = true;
}
currentDistance = !isCorrected || correctedDistance > currentDistance ? Mathf.Lerp(currentDistance, correctedDistance, Time.deltaTime * ZoomRate) : correctedDistance;
position = CameraTarget.position - (rotation * Vector3.forward * currentDistance + new Vector3(0, -cameraTargetHeight, 0));
transform.rotation = rotation;
transform.position = position;
float cameraX = transform.rotation.x;
CameraTarget.eulerAngles = new Vector3(cameraX, transform.eulerAngles.y, transform.eulerAngles.z);
}