private void RotateRigAndCameraForLookahead() { //instantiation var mousePos = Input.mousePosition; var mouseScreenX = (mousePos.x - (Screen.width * 0.5f)) / (Screen.width * 0.5f); var mouseScreenY = -(mousePos.y - (Screen.height * 0.5f)) / (Screen.height * 0.5f); // Clamp these screen position to make sure the rig doesn't oversteer. mouseScreenX = Mathf.Clamp(mouseScreenX, -1f, 1f); mouseScreenY = Mathf.Clamp(mouseScreenY, -1f, 1f); // Use the horizontal and vertical turn angles to rotate the camera's local rig position. float horizontal = 0f; float vertical = 0f; horizontal = horizontalTurnAngle * mouseScreenX; vertical = (mouseScreenY < 0.0f) ? verticalTurnUpAngle * mouseScreenY : verticalTurnDownAngle * mouseScreenY; lookAheadRig.localRotation = SmoothDamp.DampS(lookAheadRig.localRotation, Quaternion.Euler(-vertical, -horizontal, 0f), smoothSpeed, Time.deltaTime); Vector3 lookaheadPosition = ship.transform.TransformPoint(Vector3.forward * 100f); cam.transform.rotation = Quaternion.LookRotation(lookaheadPosition - lookAheadRig.position, lookAheadRig.up); }
private void RotateRigAndCameraForLookahead() { // Use the mouse position to figure out where on the screen the mouse is. When the // mouse is at extremes, the camera will turn to look in that direction, giving the // appearance of the ship swinging around in a really nice way. var mousePos = Input.mousePosition; // Normalize screen positions so that the range is -1 to 1. Makes the math easier. var mouseScreenX = (mousePos.x - (Screen.width * 0.5f)) / (Screen.width * 0.5f); var mouseScreenY = -(mousePos.y - (Screen.height * 0.5f)) / (Screen.height * 0.5f); // Clamp these screen position to make sure the rig doesn't oversteer. mouseScreenX = Mathf.Clamp(mouseScreenX, -1f, 1f); mouseScreenY = Mathf.Clamp(mouseScreenY, -1f, 1f); // Use the horizontal and vertical turn angles to rotate the camera's local rig position. float horizontal = 0f; float vertical = 0f; horizontal = horizontalTurnAngle * mouseScreenX; vertical = (mouseScreenY < 0.0f) ? verticalTurnUpAngle * mouseScreenY : verticalTurnDownAngle * mouseScreenY; // Rotate only the lookahead rig. The lookahead rig is a separate transform under the // the regular camera rig that is used only for this lookahead motion. This allows there // to easily be different "channels" of camera motion that all function independently of // each other. lookAheadRig.localRotation = SmoothDamp.DampS(lookAheadRig.localRotation, Quaternion.Euler(-vertical, -horizontal, 0f), smoothSpeed, Time.deltaTime); // After rotating the look rig, the camera needs to continue pointing forwards. // Point the camera at some point projected forwards from the ship. Vector3 lookaheadPosition = ship.transform.TransformPoint(Vector3.forward * 100f); cam.transform.rotation = Quaternion.LookRotation(lookaheadPosition - lookAheadRig.position, lookAheadRig.up); }
private void MoveCamera() { if (ship == null) { return; } // Follow the ship around. transform.position = ship.position; // Using the look rotation of the ship's forward along with the rigs own up means // that the rig will follow the ship's rotation in pitch and yaw, but NOT in roll. // This allows the ship to roll on it own. var targetRigRotation = Quaternion.LookRotation(ship.forward, transform.up); transform.rotation = SmoothDamp.DampS(transform.rotation, targetRigRotation, smoothSpeed, Time.deltaTime); }
private void MoveCamera() { if (ship == null) { return; } // Follow the ship around. transform.position = ship.position; var targetRigRotation = Quaternion.LookRotation(ship.forward, transform.up); transform.rotation = SmoothDamp.DampS(transform.rotation, targetRigRotation, smoothSpeed, Time.deltaTime); RotateRigAndCameraForLookahead(); }