public override CameraInput UpdateCamera(ref ControlReferences references) { // Obtain reference to vehicle and if not currently set, just return current input. Vehicle vehicle = references.vehicle; if (!vehicle) { return(cameraInput); } bool cameraSwitch = false; // Is it time to re-evaluate the closest CCTV camera position? if (Time.time >= nextEvaluateTime) { // Get closest position. cameraSwitch will be true if the position was actually switched. cameraSwitch = GetClosestCCTVPosition(vehicle.position, cameraInput.position, out cameraInput.position); // Set next evaluation time. nextEvaluateTime = Time.time + evaluateCameraSwitchInterval; } // Update camera rotation to look at the vehicle. // If we just switched the camera or there is no smooth time, update the rotation immediately. // If not, smoothly track the vehicle with cameraTrackSmoothTime. cameraInput.rotation = (cameraSwitch || cameraTrackSmoothTime <= 0) ? Quaternion.LookRotation(vehicle.position - cameraInput.position) : Quaternion.Slerp(cameraInput.rotation, Quaternion.LookRotation(vehicle.position - cameraInput.position), Time.deltaTime / cameraTrackSmoothTime); return(cameraInput); }
public override void Initialize(ref ControlReferences references) { // Obtain reference to vehicle and if not currently set, just return. Vehicle vehicle = references.vehicle; if (!vehicle) { return; } // Initialize the horizontal and vertical angles of the orbit camera based on the current vehicle forward direction. Vector3 vehicleForward = vehicle.forward; float vehicleAngle = Mathf.Atan2(vehicleForward.x, vehicleForward.z) * Mathf.Rad2Deg; horizontalAngle = initialHorizontalAngle + vehicleAngle; verticalAngle = initialVerticalAngle; // Init up vectors and last frame rotation to defaults. vehicleUp = Vector3.up; lastRotation = Quaternion.identity; lastUp = Vector3.up; // Set camera type (for use elsewhere) references.currentCameraType = CameraType.Orbit; }
public override CameraInput UpdateCamera(ref ControlReferences references) { // Obtain reference to vehicle and if not currently set, just return current input. Vehicle vehicle = references.vehicle; if (!vehicle) { return(cameraInput); } // Obtain current vehicle pivot point. Vector3 pivot = vehicle.orbitCameraPivotBase + vehicle.orbitCameraPivotOffset; // Calculate camera position based on orbit angles and distance Vector3 targetPosition = pivot + Quaternion.Euler(-verticalAngle, horizontalAngle, 0) * Vector3.forward * vehicle.orbitCameraDistance * orbitCameraDistanceMultiplier; Vector3 cameraVector = targetPosition - pivot; // Keep camera from hitting anything RaycastHit hit; if (Physics.Raycast(vehicle.orbitCameraPivotBase, cameraVector, out hit, cameraVector.magnitude + cameraCollisionOffset, cameraCollisionLayerMask)) { targetPosition = hit.point - cameraVector.normalized * cameraCollisionOffset; } // Update final position and rotation for camera. cameraInput.position = targetPosition; cameraInput.rotation = Quaternion.LookRotation(-cameraVector); return(cameraInput); }
public override CameraInput UpdateCamera(ref ControlReferences references) { // Obtain reference to vehicle and if not currently set, just return current input. Vehicle vehicle = references.vehicle; if (!vehicle || !vehicle.cockpitTransorm) { return(cameraInput); } // Update camera orbit angles based on player input and within min/max ranges. if (allowCameraRotation) { horizontalAngle = Mathf.Clamp(horizontalAngle + PlayerInput.cameraHorizontal, vehicle.cockpitCameraMinHorizontalAngle, vehicle.cockpitCameraMaxHorizontalAngle); verticalAngle = Mathf.Clamp(verticalAngle + PlayerInput.cameraVertical, vehicle.orbitCameraMinVerticalAngle, vehicle.orbitCameraMaxVerticalAngle); } // Update final position for camera. cameraInput.position = vehicle.cockpitTransorm.position; // Smoothly follow vehicle base cockpit rotation cockpitRotation = cockpitRotationSmoothTime > 0 ? Quaternion.Slerp(cockpitRotation, vehicle.cockpitTransorm.rotation, Time.deltaTime / cockpitRotationSmoothTime) : vehicle.cockpitTransorm.rotation; // Update final rotation for camera base on cockpit base rotation and player input. cameraInput.rotation = cockpitRotation * Quaternion.Euler(verticalAngle, horizontalAngle, 0); return(cameraInput); }
public override VehicleInput UpdateSteering(ref ControlReferences references) { // Verify there is a cemeraRigTransform before proceeding. if (!references.cameraRigTransform) { return(vehicleInput); } // Camera RIGHT/LEFT relative input. Vector3 cameraRight = PlayerInput.vehicleHorizontal * references.cameraRigTransform.right; // Camera FORWARD/BACK relative input // First calculate XY plane vectors for camera forward and up (will use up if camera is looking straight up or down). Vector3 cameraHorizontal = references.cameraRigTransform.forward; cameraHorizontal.y = 0; Vector3 cameraVertical = references.cameraRigTransform.up; cameraVertical.y = 0; // Final foward/back relative input Vector3 cameraForward = PlayerInput.vehicleVertical * (cameraHorizontal == Vector3.zero ? cameraVertical.normalized : cameraHorizontal.normalized); // Vehicle Input //--------------------------------------- vehicleInput.moveDirection = (cameraForward + cameraRight) / 2; // Throttle is the max of vehicle horizontal or vertical input (always forward in this mode currently). vehicleInput.forwardThrottle = Mathf.Max(Mathf.Abs(PlayerInput.vehicleHorizontal), Mathf.Abs(PlayerInput.vehicleVertical)); vehicleInput.handBrake = PlayerInput.handBrake; vehicleInput.speedBoost = PlayerInput.speedBoost; return(vehicleInput); }
public override void Initialize(ref ControlReferences references) { // Obtain reference to vehicle and if not currently set, just return. Vehicle vehicle = references.vehicle; if (!vehicle) { return; } // Calculate vehicle's current angles in world space (based on forward vector). Vector3 vehicleForward = vehicle.forward; float vehicleHorizontalAngle = Mathf.Atan2(vehicleForward.x, vehicleForward.z) * Mathf.Rad2Deg; float vehicleVerticalAngle = Mathf.Asin(-vehicleForward.y) * Mathf.Rad2Deg; // Calculate initial angles based on vehicle and target angles. horizontalAngle = vehicleHorizontalAngle + targetHorizontalAngle; if (followVertical) { verticalAngle = vehicleVerticalAngle + targetVerticalAngle; } else { verticalAngle = targetVerticalAngle; } // Set camera type (for use elsewhere) references.currentCameraType = CameraType.Orbit; }
public override TurretInput UpdateAiming(ref ControlReferences references) { if (references.driverCamera) { // Store reference to the driver's camera transform. Transform cameraTransform = references.driverCamera.transform; RaycastHit hit; // Aim out the front of the camera to maxTargetingDistance. // Set turrent input aimpoint to anything hit or at max distance along camera forward vector if nothing hit. if (Physics.Raycast(cameraTransform.position, cameraTransform.forward, out hit, maxTargetingDistance, aimLayerMask)) { turretInput.aimPoint = hit.point; } else { turretInput.aimPoint = cameraTransform.position + cameraTransform.forward * maxTargetingDistance; } } // Set turret firing input. turretInput.primaryFire = PlayerInput.primaryFire; turretInput.secondaryFire = PlayerInput.secondaryFire; return(turretInput); }
public override void Exit(ref ControlReferences references) { // Return the reticle to its original position. if (reticle) { reticle.rectTransform.anchoredPosition = reticleOriginalPos; } }
public override void Exit(ref ControlReferences references) { // Return reticle to the previous active state. if (reticle) { reticle.gameObject.SetActive(reticleActiveState); } }
public override CameraInput UpdateCamera(ref ControlReferences references) { // Obtain reference to vehicle and if not currently set, just return current input. Vehicle vehicle = references.vehicle; if (!vehicle) { return(cameraInput); } // Update camera orbit angles based on player input. horizontalAngle += PlayerInput.cameraHorizontal; horizontalAngle %= 360; verticalAngle = Mathf.Clamp(verticalAngle + PlayerInput.cameraVertical, vehicle.orbitCameraMinVerticalAngle, vehicle.orbitCameraMaxVerticalAngle); // Smoothly update vehicle up vector follower. vehicleUp = upSmoothTime > 0 ? Vector3.Slerp(vehicleUp, vehicle.up, Time.deltaTime / upSmoothTime) : vehicle.up; // Calculate vehicle's up rotation (based on smoothed vehicleUp and the last frames settings - incremental updates. Quaternion vehicleUpRotation = FromToRotation(lastUp, vehicleUp) * lastRotation; lastRotation = vehicleUpRotation; lastUp = vehicleUp; // Obtain current vehicle pivot point. Vector3 pivot = vehicle.orbitCameraPivotBase + vehicleUpRotation * vehicle.orbitCameraPivotOffset + vehicleUpRotation * Quaternion.Euler(0, horizontalAngle, 0) * offset; // Calculate camera position based on orbit angles and distance Vector3 targetPosition = pivot + Quaternion.Euler(-verticalAngle, horizontalAngle, 0) * Vector3.forward * vehicle.orbitCameraDistance * orbitCameraDistanceMultiplier; // Modify camera position if it goes below vehicle's camera floor (cap it at the floor bottom). Vector3 cameraVector = targetPosition - pivot; if (cameraVector.y < vehicle.orbitCameraFloor) { float ratio = vehicle.orbitCameraFloor / cameraVector.y; targetPosition = pivot + cameraVector * ratio; cameraVector = targetPosition - pivot; } // Update targetPosition based on the vehicle's Up rotation. targetPosition = pivot + vehicleUpRotation * cameraVector; // Keep camera from hitting anything RaycastHit hit; Vector3 targetVector = targetPosition - pivot; if (Physics.Raycast(pivot, targetVector, out hit, targetVector.magnitude + cameraCollisionOffset, cameraCollisionLayerMask)) { targetPosition = hit.point - targetVector.normalized * cameraCollisionOffset; } // Update final position and rotation for camera. cameraInput.position = targetPosition; cameraInput.rotation = vehicleUpRotation * Quaternion.LookRotation(-cameraVector); return(cameraInput); }
public override void Initialize(ref ControlReferences references) { // Obtain the aiming reticle's current active state, then turn off for this controller. if (reticle) { reticleActiveState = reticle.IsActive(); reticle.gameObject.SetActive(false); } }
public override void Initialize(ref ControlReferences references) { // Initialize horizontal and vertial angles. horizontalAngle = initialHorizontalAngle; verticalAngle = initialVerticalAngle; // Set camera type (for use elsewhere) references.currentCameraType = CameraType.Orbit; }
public override CameraInput UpdateCamera(ref ControlReferences references) { // Obtain reference to vehicle and if not currently set, just return current input. Vehicle vehicle = references.vehicle; if (!vehicle) { return(cameraInput); } // Calculate vehicle's current angles in world space Vector3 vehicleForward = vehicle.forward; float vehicleHorizontalAngle = Mathf.Atan2(vehicleForward.x, vehicleForward.z) * Mathf.Rad2Deg; float vehicleVerticalAngle = Mathf.Asin(-vehicleForward.y) * Mathf.Rad2Deg; // Update camera follow angles smoothly towards target angles. horizontalAngle = Mathf.SmoothDampAngle(horizontalAngle, vehicleHorizontalAngle + targetHorizontalAngle, ref horizontalAngleVelocity, horizontalSmoothTime); if (followVertical) { verticalAngle = Mathf.SmoothDamp(verticalAngle, vehicleVerticalAngle + targetVerticalAngle, ref verticalAngleVelocity, verticalSmoothTime); } else { verticalAngle = Mathf.SmoothDamp(verticalAngle, targetVerticalAngle, ref verticalAngleVelocity, verticalSmoothTime); } // Obtain current vehicle pivot point. Vector3 pivot = vehicle.orbitCameraPivotBase + vehicle.orbitCameraPivotOffset; // Calculate camera position based on pivot position and orbit angles and distance Vector3 targetPosition = pivot + Quaternion.Euler(-verticalAngle, horizontalAngle, 0) * Vector3.forward * vehicle.orbitCameraDistance * orbitCameraDistanceMultiplier; Vector3 cameraVector = targetPosition - pivot; // Keep camera from hitting anything RaycastHit hit; if (Physics.Raycast(pivot, cameraVector, out hit, cameraVector.magnitude + cameraCollisionOffset, cameraCollisionLayerMask)) { targetPosition = hit.point - cameraVector.normalized * cameraCollisionOffset; } // Update final position of camera. cameraInput.position = targetPosition; // Update rotation, which is just a look at the pivot point rotation. cameraInput.rotation = Quaternion.LookRotation(-cameraVector, Vector3.up); // If looking straight up or down, the "up" axis for LookRotation will be based on the horizontal angle. Vector3 up = Mathf.Abs(verticalAngle) != 90 ? Vector3.up : Quaternion.Euler(0, horizontalAngle, 0) * -Vector3.forward; // Calculate rotation (look at rotation). cameraInput.rotation = Quaternion.LookRotation(-cameraVector, up); return(cameraInput); }
public override VehicleInput UpdateSteering(ref ControlReferences references) { // Set vehicle's relative move direction based on player left/right input. vehicleInput.relativeMoveDirection = Quaternion.AngleAxis(PlayerInput.vehicleHorizontal * 90, Vector3.up) * Vector3.forward; // Set vehicle inputs based on player input. vehicleInput.forwardThrottle = PlayerInput.vehicleVertical; vehicleInput.handBrake = PlayerInput.handBrake; vehicleInput.speedBoost = PlayerInput.speedBoost; return(vehicleInput); }
public override VehicleInput UpdateSteering(ref ControlReferences references) { // Camera move direction based on the player driver camera rig forward vector. if (references.cameraRigTransform) { vehicleInput.moveDirection = references.cameraRigTransform.forward; } // Set vehicle inputs based on player input. vehicleInput.forwardThrottle = PlayerInput.vehicleVertical; vehicleInput.sideThrottle = PlayerInput.vehicleHorizontal; vehicleInput.handBrake = PlayerInput.handBrake; vehicleInput.speedBoost = PlayerInput.speedBoost; return(vehicleInput); }
public override void Initialize(ref ControlReferences references) { // Obtain reference to vehicle and if not currently set, just return. Vehicle vehicle = references.vehicle; if (!vehicle) { return; } // Get the closest CCTV camera position to the vehicle and set rotation to look at the vehicle. GetClosestCCTVPosition(vehicle.position, cameraInput.position, out cameraInput.position); cameraInput.rotation = Quaternion.LookRotation(vehicle.position - cameraInput.position); // Set camera type (for use elsewhere) references.currentCameraType = CameraType.Stationary; }
public override CameraInput UpdateCamera(ref ControlReferences references) { // Obtain reference to vehicle and if not currently set, just return current input. Vehicle vehicle = references.vehicle; if (!vehicle) { return(cameraInput); } // Smoothly update vehicle forward and up vector followers. vehicleForward = fowardSmoothTime > 0 ? Vector3.Slerp(vehicleForward, vehicle.forward, Time.deltaTime / fowardSmoothTime) : vehicle.forward; vehicleUp = upSmoothTime > 0 ? Vector3.Slerp(vehicleUp, vehicle.up, Time.deltaTime / upSmoothTime) : vehicle.up; // Update the followVector based on smoothed vehicle forward and up follow vectors. followVector = Quaternion.LookRotation(vehicleForward, vehicleUp) * targetFollowVector * vehicle.orbitCameraDistance * orbitCameraDistanceMultiplier; // Calculate vehicle's up rotation (based on smoothed vehicleUp and the last frames settings - incremental updates. Quaternion vehicleUpRotation = FromToRotation(lastUp, vehicleUp) * lastRotation; lastRotation = vehicleUpRotation; lastUp = vehicleUp; // Obtain current vehicle pivot point. Vector3 pivot = vehicle.orbitCameraPivotBase + vehicleUpRotation * vehicle.orbitCameraPivotOffset; // Calculate camera position based on orbit angles and distance Vector3 targetPosition = pivot + followVector; Vector3 cameraVector = targetPosition - pivot; // Keep camera from hitting anything RaycastHit hit; if (Physics.Raycast(pivot, cameraVector, out hit, cameraVector.magnitude + cameraCollisionOffset, cameraCollisionLayerMask)) { targetPosition = hit.point - cameraVector.normalized * cameraCollisionOffset; } // Update final position and rotation for camera. cameraInput.position = targetPosition; cameraInput.rotation = Quaternion.LookRotation(-cameraVector, vehicleUp); return(cameraInput); }
public override CameraInput UpdateCamera(ref ControlReferences references) { // Obtain reference to vehicle and if not currently set, just return current input. Vehicle vehicle = references.vehicle; if (!vehicle) { return(cameraInput); } // Update camera orbit angles based on player input. horizontalAngle += PlayerInput.cameraHorizontal; horizontalAngle %= 360; verticalAngle = Mathf.Clamp(verticalAngle + PlayerInput.cameraVertical, vehicle.orbitCameraMinVerticalAngle, vehicle.orbitCameraMaxVerticalAngle); // Get adjusted pivot point based on offset and rotation. Vector3 pivot = vehicle.orbitCameraPivotBase + vehicle.orbitCameraPivotOffset + Quaternion.Euler(0, horizontalAngle, 0) * offset; // Calculate camera position based on orbit angles and distance Vector3 targetPosition = pivot + Quaternion.Euler(-verticalAngle, horizontalAngle, 0) * Vector3.forward * vehicle.orbitCameraDistance * orbitCameraDistanceMultiplier; // Modify camera position if it goes below vehicle's orbit camera floor (cap it at the floor bottom). Vector3 cameraVector = targetPosition - pivot; if (cameraVector.y < vehicle.orbitCameraFloor) { float ratio = vehicle.orbitCameraFloor / cameraVector.y; targetPosition = pivot + cameraVector * ratio; cameraVector = targetPosition - pivot; } // Keep camera from hitting anything RaycastHit hit; if (Physics.Raycast(pivot, cameraVector, out hit, cameraVector.magnitude + cameraCollisionOffset, cameraCollisionLayerMask)) { targetPosition = hit.point - cameraVector.normalized * cameraCollisionOffset; } // Update final position and rotation for camera. cameraInput.position = targetPosition; cameraInput.rotation = Quaternion.LookRotation(-cameraVector); return(cameraInput); }
public override TurretInput UpdateAiming(ref ControlReferences references) { if (references.driverCamera) { // Get reference to driver's camera. Camera driverCamera = references.driverCamera; // Update screen width and height since the screen size can change. screenWidth = driverCamera.pixelWidth; screenHeight = driverCamera.pixelHeight; // Calculate new position of reticle based on player input. targetPos.x = Mathf.Clamp(targetPos.x + PlayerInput.cameraHorizontal * targetMoveSpeed, 0f, screenWidth); targetPos.y = Mathf.Clamp(targetPos.y - PlayerInput.cameraVertical * targetMoveSpeed, 0f, screenHeight); // Place reticle at new position. if (reticle) { reticle.rectTransform.anchoredPosition = targetPos - new Vector2(screenWidth / 2f, screenHeight / 2f); } // Cast a ray out of the driver's camera to find a target. RaycastHit hit; Ray ray = driverCamera.ScreenPointToRay(targetPos); // Aim out the front of the camera to maxTargetingDistance. // Set turrent input aimpoint to anything hit or at max distance along camera forward vector if nothing hit. if (Physics.Raycast(ray, out hit, maxTargetingDistance, aimLayerMask)) { turretInput.aimPoint = hit.point; } else { turretInput.aimPoint = ray.GetPoint(maxTargetingDistance); } } // Set turret firing input. turretInput.primaryFire = PlayerInput.primaryFire; turretInput.secondaryFire = PlayerInput.secondaryFire; return(turretInput); }
public override void Initialize(ref ControlReferences references) { // If a reticle specified, obtain it's initial position in screen space. if (reticle) { reticleOriginalPos = reticle.rectTransform.anchoredPosition; } // Save screen width and height (in pixels) of the driver's camera. if (references.driverCamera) { // Obtain screen width and height to set initial reticle position. screenWidth = references.driverCamera.pixelWidth; screenHeight = references.driverCamera.pixelHeight; // Set initial position of reticle to the middle of the screen. targetPos = new Vector2(screenWidth / 2f, screenHeight / 2f); } }
public override TurretInput UpdateAiming(ref ControlReferences references) { // Verify there is a vehicle and cemeraRigTransform before proceeding. if (!references.vehicle || !references.cameraRigTransform) { return(turretInput); } // If player input is past the threshold, re-obtain input for aiming direction calculation. if (Mathf.Abs(PlayerInput.cameraHorizontal) > inputThreshold || Mathf.Abs(PlayerInput.cameraVertical) > inputThreshold) { playerHorizontalInput = PlayerInput.cameraHorizontal; playerVerticalInput = PlayerInput.cameraVertical; } // Camera RIGHT/LEFT relative input. Vector3 cameraRight = playerHorizontalInput * references.cameraRigTransform.right; // Camera FORWARD/BACK relative input // First calculate XY plane vectors for camera forward and up (will use up if camera is looking straight up or down). Vector3 cameraHorizontal = references.cameraRigTransform.forward; cameraHorizontal.y = 0; Vector3 cameraVertical = references.cameraRigTransform.up; cameraVertical.y = 0; // Final foward/back relative input Vector3 cameraForward = -playerVerticalInput * (cameraHorizontal == Vector3.zero ? cameraVertical.normalized : cameraHorizontal.normalized); // Obtain the final aim vector based on player input (average of left/right and forward/back input) Vector3 aimVector = (cameraForward + cameraRight) / 2; // Turret Input //--------------------------------------- // Set the turret aimpoint off of the vehicle's position and aimDistanceFromVehicle along the aim vector. turretInput.aimPoint = references.vehicle.position + aimVector.normalized * aimDistanceFromVehicle; // Set turret firing input. turretInput.primaryFire = PlayerInput.primaryFire; turretInput.secondaryFire = PlayerInput.secondaryFire; return(turretInput); }
public override void Initialize(ref ControlReferences references) { // Initialize camera rotation angles. horizontalAngle = 0; verticalAngle = 0; // Initialize the base cockpit transform from the vehicle if specified. if (references.vehicle && references.vehicle.cockpitTransorm) { cockpitRotation = references.vehicle.cockpitTransorm.rotation; } else { Debug.LogWarning("No Cockpit Transform specified with vehicle for CockpitCamera on " + name); } // Set camera type (for use elsewhere) references.currentCameraType = CameraType.Cockpit; }
public override void Initialize(ref ControlReferences references) { // Obtain reference to vehicle and if not currently set, just return. Vehicle vehicle = references.vehicle; if (!vehicle) { return; } // Calculate the target follow vector (based on target angles) targetFollowVector = Quaternion.Euler(-targetVerticalAngle, targetHorizontalAngle, 0) * Vector3.forward; // Calculate initial follow vector based on target and vehicle's forward. followVector = Quaternion.LookRotation(vehicle.forward, vehicle.up) * targetFollowVector; // Init up vectors and last frame rotation to defaults. vehicleUp = Vector3.up; lastRotation = Quaternion.identity; lastUp = Vector3.up; // Set camera type (for use elsewhere) references.currentCameraType = CameraType.Orbit; }
public abstract CameraInput UpdateCamera(ref ControlReferences references);
public override void Exit(ref ControlReferences references) { }
public abstract VehicleInput UpdateSteering(ref ControlReferences references);
public override void Initialize(ref ControlReferences references) { }
// Exit the controller. // Called when the controller is no longer used - perform any cleanup necessary. public abstract void Exit(ref ControlReferences references);
// Initialize the controller. // Called when the controller is about to be used and should initialize any state and/or variables needed. public abstract void Initialize(ref ControlReferences references);
public abstract TurretInput UpdateAiming(ref ControlReferences references);