void Update() { if (InputState.CameraDisabled()) { return; } bool mouseLookDown = Input.GetMouseButton((int)mouseLookButton); bool mouseLookPressed = Input.GetMouseButtonDown((int)mouseLookButton); if (mouseLookDown) { RotateCameraBehindTarget = true; LockCameraBehindTarget = true; float angles = Input.GetAxisRaw("Mouse X") * Time.smoothDeltaTime * mouseTurnSpeed; if (target != null) { target.RotateAround(Vector3.up, angles); } } // If we pressed mouse look, set rotation if (mouseLookPressed) { } }
void LateUpdate() { if (InputState.CameraDisabled()) { return; } if (!HasCamera) { Debug.LogError("No camera found"); return; } if (!HasTarget) { //Debug.LogWarning("No target found"); Initialize(); return; } bool rotate = GetButtonSafe(MouseRotateButton, false); bool mouseLook = GetButtonSafe(MouseLookButton, false); bool smoothRotation = SmoothRotation || SmoothAutoRotation; float smoothRotationSpeed = SmoothRotationSpeed; // This defines our "real" distance to the player realDistance -= GetAxisRawSafe(ZoomAxis, 0f) * ZoomSpeed; realDistance = Mathf.Clamp(realDistance, MinDistance, MaxDistance); // This is the distance we want to (clamped to what is viewable) targetDistance = realDistance; targetDistance = Mathf.Clamp(targetDistance, currentMinDistance, currentMaxDistance); // This is our current distance if (SmoothZoom) { currentDistance = Mathf.Lerp(currentDistance, targetDistance, Time.deltaTime * SmoothZoomSpeed); } else { currentDistance = targetDistance; } // Calculate offset vector Vector3 offset = new Vector3(0, 0, -currentDistance); // LMB is not down, but we should rotate camera behind target if (!rotate && RotateCameraBehindTarget) { targetYaw = SignedAngle(offset.normalized, -target.forward, Vector3.up); smoothRotation = SmoothAutoRotation; smoothRotationSpeed = SmoothAutoRotationSpeed; } // Only LMB down and no lock if (rotate && !mouseLook && !LockCameraBehindTarget) { targetYaw += (GetAxisRawSafe(YawAxis, 0f) * RotationMouseSpeed); targetPitch -= (GetAxisRawSafe(PitchAxis, 0f) * RotationMouseSpeed); targetPitch = Mathf.Clamp(targetPitch, MinPitch, MaxPitch); smoothRotation = SmoothRotation; smoothRotationSpeed = SmoothRotationSpeed; } // RMB if (mouseLook && LockCameraBehindTarget) { targetPitch -= (GetAxisRawSafe(PitchAxis, 0f) * RotationMouseSpeed); targetPitch = Mathf.Clamp(targetPitch, MinPitch, MaxPitch); } // Lock camera behind target, this overrides everything if (LockCameraBehindTarget) { targetYaw = SignedAngle(offset.normalized, -target.transform.forward, Vector3.up); smoothRotation = false; } // Clamp targetYaw to -180, 180 targetYaw = Mathf.Repeat(targetYaw + 180f, 360f) - 180f; smoothRotation = true; if (!smoothRotation) { currentYaw = targetYaw; currentPitch = targetPitch; } else { // Clamp smooth currentYaw to targetYaw and clamp it to -180, 180 currentYaw = Mathf.LerpAngle(currentYaw, targetYaw, Time.deltaTime * smoothRotationSpeed); currentYaw = Mathf.Repeat(currentYaw + 180f, 360f) - 180f; // Smooth pitch currentPitch = Mathf.LerpAngle(currentPitch, targetPitch, Time.deltaTime * smoothRotationSpeed); } // Rotate offset vector offset = Quaternion.Euler(currentPitch, currentYaw, 0f) * offset; // Position camera holder correctly transform.position = TargetPosition + offset; // And then have the camera look at our target cam.transform.LookAt(TargetPosition); // Make sure we don't collide with anything float closest = float.MaxValue; bool mid = AvoidCollision(transform.position, ref closest); bool bottomLeft = AvoidCollision(cam.ScreenToWorldPoint(new Vector3(0, 0, cam.nearClipPlane)), ref closest); bool bottomRight = AvoidCollision(cam.ScreenToWorldPoint(new Vector3(0, Screen.height, cam.nearClipPlane)), ref closest); bool topLeft = AvoidCollision(cam.ScreenToWorldPoint(new Vector3(Screen.width, 0, cam.nearClipPlane)), ref closest); bool topRight = AvoidCollision(cam.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, cam.nearClipPlane)), ref closest); if (mid && bottomLeft && bottomRight && topLeft && topRight) { currentMinDistance = MinDistance; currentMaxDistance = MaxDistance; } else { currentMinDistance = Mathf.Min(currentMinDistance, 1f); currentMaxDistance = Mathf.Max(currentMinDistance + 0.05f, closest * 0.9f); } // Clear this flag LockCameraBehindTarget = false; RotateCameraBehindTarget = false; }
public bool MovementDisabled() { return(InputState.CameraDisabled()); }