void Zoom(float value) { if (value != 0) { idealDistance = zoomDistance.Clamp(distance - value * zoomSpeed); } }
void FixedUpdate() { if (angleLimits.IsOutside(rigidbody.rotation)) { rigidbody.rotation = angleLimits.Clamp(rigidbody.rotation); } }
void Update() { if (PlayerCamera.enabled) { this.angleX = MinMaxRotationX.Clamp(this.angleX - Input.GetAxis("Mouse Y")); this.angleY = MinMaxRotationY.Clamp(this.angleY + Input.GetAxis("Mouse X")); this.cameraDistance = MinMaxCameraDistance.Clamp(this.cameraDistance + Input.GetAxis("Mouse ScrollWheel") * 10f); Quaternion upRotation = Quaternion.LookRotation(new Vector3(target.forward.x, 0f, target.forward.z)) * Quaternion.Euler(0f, this.angleY, 0f); transform.position = target.position + Vector3.Scale(upRotation * normal, new Vector3(1, this.angleX / 25, 1)) * this.cameraDistance; transform.rotation = Quaternion.LookRotation((target.position + offSetLookAt) - transform.position) * Quaternion.Euler(this.angleX, 0f, 0f); //Debug.Log(transform.position); } }
void LateUpdate() { if (target) { float clampedX = Mathf.Clamp(Input.GetAxis("Mouse X") * (idealDistance / distance), -mouseSpeedLimit.x, mouseSpeedLimit.x); // Avoid going too fast (makes weird lerp) yaw += clampedX * rotationSpeed.x * distance * 0.02f; float clampedY = Mathf.Clamp(Input.GetAxis("Mouse Y") * (idealDistance / distance), -mouseSpeedLimit.y, mouseSpeedLimit.y); // Avoid going too fast (makes weird lerp) pitch -= clampedY * rotationSpeed.y * distance * 0.02f; pitch = pitchRotationLimit.Clamp(pitch); Quaternion rotation = Quaternion.Euler(pitch, yaw, 0); if (canZoom) { Zoom(Input.GetAxis("Mouse ScrollWheel")); } offset.x = Mathf.Lerp(offsetClose.x, offsetFar.x, distance / maxDistance); offset.y = Mathf.Lerp(offsetClose.y, offsetFar.y, distance / maxDistance); Vector3 targetPosition = target.position; if (offsetOverriden) { targetPosition.x += _tempOffset.x; targetPosition.y += _tempOffset.y; _tempOffset = Vector2.Lerp(_tempOffset, Vector2.zero, Time.deltaTime * cameraSpeed); if (Vector2.Distance(_tempOffset, Vector2.zero) < .01f) { offsetOverriden = false; } } camRotation = rotation; CheckForCollision(); negDistance.z = -distance; Vector3 targetWithOffset = targetPosition + my.right * offset.x + my.up * offset.y; camPosition = rotation * negDistance + targetWithOffset; //my.LookAt(targetWithOffset); SmoothMovement(); target.rotation = Quaternion.Euler(0, yaw, 0); // Reoriente the character's rotator } }
void Update() { Int2 movementInput = InputHelper.GetWASDMovement(); int elevationInput = Convert.ToInt32(Input.GetKey(KeyCode.E)) - Convert.ToInt32(Input.GetKey(KeyCode.Q)); if (movementInput == Int2.zero && elevationInput == 0) { return; } Elevation = elevationRange.Clamp(Elevation + elevationInput * elevationSpeed * Time.deltaTime); Position += movementInput * Elevation * movementSpeed * Time.deltaTime; OnReoriented?.Invoke(); }
private void SetTranslateToPosition() { float randDeltaX = Random.Range(_DeltaX, -_DeltaX); float randDeltaY = Random.Range(_DeltaY, -_DeltaY); float newX = mOriginalPosition.x + randDeltaX; newX = mXOffset.Clamp(newX); float newY = mOriginalPosition.y + randDeltaY; newY = mYOffset.Clamp(newY); mTranslateTo = new Vector3(newX, newY, mOriginalPosition.z); mTime = 0.0f; }
/* * Updates the position of the camera */ private void UpdatePosition() { // Figure out new zoom float scroll = 12f * Input.GetAxis("Mouse ScrollWheel"); // Search for rotation input // We only want to turn the camera if we are zoomed in if (Input.GetButton("Rotate")) { transform.RotateAround(transform.position, Vector3.up, -1.0f * Input.GetAxisRaw("Rotate")); } // Store the current rotation for when we apply the XRotation Lerp Vector3 currentRotation = transform.rotation.eulerAngles; // Figure out how much distance the pan should cover // Multiplied by unscaledDeltaTime so pausing and fast forwarding does not affect camera movement float panDistance = panSpeed * Time.unscaledDeltaTime; // Pan depending on which keys have been pressed (or which borders the mouse is currently in) if (Input.GetButton("Vertical") || (panWithMouse && (Input.mousePosition.y >= Screen.height - panBorderThickness || Input.mousePosition.y <= panBorderThickness))) { if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S)) { // Calculate the perpendicular axis of travel for the camera based on its current rotation Vector3 forward = Vector3.Cross(transform.right, Vector3.up); transform.Translate(forward * Input.GetAxisRaw("Vertical") * panDistance, Space.World); } else { transform.Translate(Vector3.forward * panDistance * Input.GetAxisRaw("Vertical"), Space.World); } } if (Input.GetButton("Horizontal") || (panWithMouse && (Input.mousePosition.x <= panBorderThickness || Input.mousePosition.x >= Screen.width - panBorderThickness))) { if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D)) { Vector3 right = Vector3.Cross(Vector3.up, transform.up); transform.Translate(right * panDistance * Input.GetAxisRaw("Horizontal"), Space.World); } else { transform.Translate(Vector3.right * panDistance * Input.GetAxisRaw("Horizontal"), Space.World); } } //Search for the zoom buttons (Q and E) if (Input.GetButton("Zoom")) { //Use the axis of zoom to determine which way to zoom scroll = 0.1f * 100.0f * Input.GetAxisRaw("Zoom"); transform.Translate(Vector3.down * scroll * zoomSpeed, Space.World); } //Rotate on the x axis based on keyboard input. if (Input.GetKey(KeyCode.C) && currentRotation.x > xRotateMin) { /* * Fancier camera movement, not finished yet! * RaycastHit hit; * if(Physics.Raycast(gameObject.transform.position, gameObject.transform.forward, out hit, 10000)) * { * transform.RotateAround(hit.point, Vector3.right, -xRotateSpeed * Time.fixedDeltaTime); * } * else * { * Debug.LogError("Camera X rotation raycast didn't find an object!"); * } */ //Change rotation based on time between frames float rx = currentRotation.x - xRotateSpeed * Time.unscaledDeltaTime; if (rx < xRotateMin) { rx = xRotateMin; } transform.rotation = Quaternion.Euler(rx, currentRotation.y, currentRotation.z); if (currentRotation.x < xRotateMin) { transform.rotation = Quaternion.Euler(xRotateMin, currentRotation.y, currentRotation.z); } } else if (Input.GetKey(KeyCode.V) && currentRotation.x < xRotateMax) { /* * Fancier camera movement, not finished yet! * RaycastHit hit; * if (Physics.Raycast(gameObject.transform.position, gameObject.transform.forward, out hit, 10000)) * { * transform.RotateAround(hit.point, Vector3.left, -xRotateSpeed * Time.fixedDeltaTime); * } * else * { * Debug.LogError("Camera X rotation raycast didn't find an object!"); * } */ //Change rotation based on time between frames float rx = currentRotation.x + xRotateSpeed * Time.unscaledDeltaTime; if (rx > xRotateMax) { rx = xRotateMax; } transform.rotation = Quaternion.Euler(rx, currentRotation.y, currentRotation.z); if (currentRotation.x > xRotateMax) { transform.rotation = Quaternion.Euler(xRotateMax, currentRotation.y, currentRotation.z); } } // Clamp Pan (XZ) / Zoom (Y) within boundaries transform.position = bounds.Clamp(transform.position); }