Search Unity

Rotate on X axis only with screen edges

Discussion in 'Cinemachine' started by Duende, Oct 14, 2019.

  1. Duende

    Duende

    Joined:
    Oct 11, 2014
    Posts:
    200
    Hello, I'm experimenting with Cinemachine and I'm trying (unsuccessfully) for the camera to move only on the X-axis when the mouse is on the edges of the screen. For example, if the mouse is on the right edge of the screen it will rotate on the X axis to the right.
    Right now the camera always rotates on the X axis no matter where the mouse is, just by moving it.

    I am using Freelook and a camera with Perspective.

    How can I get this with Cinemachine?
     
  2. Duende

    Duende

    Joined:
    Oct 11, 2014
    Posts:
    200
    I managed to do it this way by looking and mixing at other forum posts:

    Code (CSharp):
    1. using UnityEngine;
    2. using Cinemachine;
    3.  
    4. public class CMFreelookEdgeCamera : MonoBehaviour {
    5.  
    6.     public int boundary = 50; // distance from edge scrolling starts
    7.     public float speed = 1f;
    8.     int screenWidth;
    9.     int screenHeight;
    10.  
    11.     void Start() {
    12.         CinemachineCore.GetInputAxis = GetAxisCustom;
    13.         screenWidth = Screen.width;
    14.         screenHeight = Screen.height;
    15.     }
    16.     public float GetAxisCustom(string axisName) {
    17.         if (axisName == "Mouse X") {
    18.             if (Input.mousePosition.x > screenWidth - boundary) {
    19.                 return speed;
    20.             }
    21.             if (Input.mousePosition.x < boundary) {
    22.                 return -speed;
    23.             }
    24.         } else if (axisName == "Mouse ScrollWheel") {
    25.             return Input.GetAxis(axisName);
    26.         }
    27.         return 0;
    28.      }
    29. }
    Thus the camera only rotates on the X axis when the mouse is on an edge and the Y axis only goes up and down using the wheel (in the CM Freelook inspector you have to write "Mouse ScrollWheel" in Axis Control / Y Axis / Input Axis Name):

    upload_2019-10-14_16-14-30.png

    But I have another question. Is it possible with Cinemachine to have a truly free look? I mean, in Cinemachine Free Look the camera moves between the 3 positions TopRig, MiddleRig and BottomRig, and, for example, if you want to look down or to the sky you can only do it if any of those 3 positions are set to look at the sky. Would a completely free look be possible using the current camera position as a pivot?

    What I want to do is mix this script that I posted with a free look by pressing the mouse button. So that the player can see around him and look at the ground or the sky at maintain press of the mouse button, but when he releases it the camera returns to its natural state in Cinemachine. But I don't know how to make that look totally free with Cinemachine and I haven't found information about this.
     
    Gregoryl likes this.
  3. Duende

    Duende

    Joined:
    Oct 11, 2014
    Posts:
    200
    Here is the solution:
    https://forum.unity.com/threads/how-to-make-a-aim-camera-with-cinemachine.710018/

    I mixed the script of the example of that post and created two Cinemachine, one with FreeLook and the other Virtual Camera with Flaming Transposer + POV.
     
    Gregoryl likes this.
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    Excellent detective work!
     
    Duende likes this.