コード例 #1
0
    void HandleCameraMovement()
    {
        Vector3 cameraAdjustmentVector;

        // On PC, the cursor point is the mouse position
        var cursorScreenPosition = MyInput.GetMousePosition();

        var halfWidth  = Screen.width / 2.0f;
        var halfHeight = Screen.height / 2.0f;
        var maxHalf    = Mathf.Max(halfWidth, halfHeight);

        // Acquire the relative screen position
        var posRel = cursorScreenPosition - new Vector3(halfWidth, halfHeight, cursorScreenPosition.z);

        posRel.x /= maxHalf;
        posRel.y /= maxHalf;

        cameraAdjustmentVector   = posRel.x * m_screenMovementRight + posRel.y * m_screenMovementForward;
        cameraAdjustmentVector.y = 0.0f;

        // Set the target position of the camera to point at the focus point
        var cameraTargetPosition = transform.position + m_cameraOffsetToPlayer + cameraAdjustmentVector * m_cameraPreview;

        // Apply some smoothing to the camera movement
        m_cameraTransform.position = Vector3.SmoothDamp(
            m_cameraTransform.position,
            cameraTargetPosition,
            ref m_cameraVelocity,
            m_cameraSmoothing);
    }
コード例 #2
0
    void HandleRigidbodyRotation()
    {
        var cursorScreenPosition = MyInput.GetMousePosition();
        var cursorWorldPosition  = Utils.ScreenPointToWorldPointOnPlane(cursorScreenPosition, m_playerMovementPlane, Camera.main);

        // The facing direction is the direction from the character to the cursor world position
        var facingDirection = (cursorWorldPosition - transform.position);

        facingDirection.y = 0;

        // Make the character rotate towards the target rotation
        var newRotatation = Quaternion.LookRotation(facingDirection);

        transform.rotation = newRotatation;
    }