Search Unity

C# Moving object on x axis with mouse move

Discussion in '2D' started by kittik, Apr 15, 2015.

  1. kittik

    kittik

    Joined:
    Mar 6, 2015
    Posts:
    565
    Hi, I am trying to create a game where the player drops a cube from the top of the screen to the bottom, collecting points on the way down depending on where they dropped the cube from (like a Coin Pusher arcade game).

    At the moment I am able to have my cube move to the mouse, but this in both X and Y values. See -
    Camera.main.ScreenPointToRay(Input.mousePosition);
    Vector3 mouse = Input.mousePosition;
    mouse = mouse;
    transform.position = Camera.main.ScreenToViewportPoint(mouse);


    I am keen to have movements only on X but am unsure how to achieve this.

    Thank you,

    Kittik
     
  2. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    Just use the result of ScreenToViewportPoint(), create a new Vector3 from it, but leave the Y at 0.
     
  3. kittik

    kittik

    Joined:
    Mar 6, 2015
    Posts:
    565
    I have 'fixed' the issue by changing the first posts code to:
    Vector3 mouse = Camera.main.ScreenToViewportPoint(Input.mousePosition);
    transform.position = new Vector3(mouse.x, transform.position.y, transform.position.z);

    But the movement from a mouse is very slow and not very responsive. Is there a way to speed up, or more accurately follow my mouse movements?
     
    Last edited: Apr 15, 2015
    hahahpizza likes this.
  4. Tomer-Barkan

    Tomer-Barkan

    Joined:
    Jul 31, 2012
    Posts:
    150
    Sounds to me like low framerate not an issue with this code specifically. What's your framerate? (click on stats in the play window)
     
  5. Toddweiss

    Toddweiss

    Joined:
    Oct 30, 2016
    Posts:
    2
    lol its the code