Search Unity

Video camera moving glitch

Discussion in 'Audio & Video' started by connellklel101, Mar 31, 2020.

  1. connellklel101

    connellklel101

    Joined:
    Mar 26, 2020
    Posts:
    3
    why does my cammera glitch back to its originak position when i try to move my mouse it moves the camera the way i want it but only for a split second until it looks back to the original position anyone get a clue whats wrong
    public class Mousemovement : MonoBehaviour
    {
    public float speedX = -2.0f;
    public float speedY = -2.0f;


    void Update()
    {
    float X = speedX * Input.GetAxis("Mouse X");
    float Y = speedY * Input.GetAxis("Mouse Y");


    transform.eulerAngles = new Vector3(Y, X, 0);
    }
    }
     
  2. marc_tanenbaum

    marc_tanenbaum

    Unity Technologies

    Joined:
    Oct 22, 2014
    Posts:
    637
    Hi there! And welcome!

    This is not really a video question. In Unity, video really refers to streaming of things like video files. Nevertheless, let's see if we can help.

    So the problem has to do with what Input.GetAxis actually does. If you drop this code into your file, you'll see what I mean:
    Code (CSharp):
    1.     private void OnGUI()
    2.     {
    3.         float X = Input.GetAxis("Mouse X");//speedX * Input.GetAxis("Mouse X");
    4.         float Y = Input.GetAxis("Mouse Y");//speedY * Input.GetAxis("Mouse Y");
    5.         string msg = string.Format("{0}, {1}", X, Y);
    6.         GUI.TextArea(new Rect(0f, 0f, 200f, 30f), msg);
    7.     }
    8.  
    With that code running, you'll get a box showing the value of the input...you'll see that it pretty much always reverts to 0 if the mouse isn't moving. I suspect what you need is something to get distance from screen center. Something like:

    Code (CSharp):
    1. float X = Input.mousePosition.x - (Screen.width / 2f);
    2. float Y = Input.mousePosition.y - (Screen.height / 2f);
    With all that said, you might want to consider using Cinemachine...which is our solution for most camera purposes. There's a bit of a learning curve, but it'll serve you better in the long run.
     
  3. connellklel101

    connellklel101

    Joined:
    Mar 26, 2020
    Posts:
    3
    all my code is move the camera and then it returns to the same position so i don't get anywhere with that and the second code you gave me i dont know where to put it
     
  4. marc_tanenbaum

    marc_tanenbaum

    Unity Technologies

    Joined:
    Oct 22, 2014
    Posts:
    637
    Your code doesn't move the camera because the values always end up as zero.

    Try replacing this in your code:
    Code (CSharp):
    1. float X = speedX * Input.GetAxis("Mouse X");
    2. float Y = speedY * Input.GetAxis("Mouse Y");
    with mine:
    Code (CSharp):
    1. float X = Input.mousePosition.x - (Screen.width / 2f);
    2. float Y = Input.mousePosition.y - (Screen.height / 2f);
    And see the difference.
     
  5. connellklel101

    connellklel101

    Joined:
    Mar 26, 2020
    Posts:
    3
    there is a big difference thank you it moves but there still is a glitch where it constantly moves but thats probably on my part of the code