Search Unity

Moving an in-game cursor using the player's current mouse position?

Discussion in 'Immediate Mode GUI (IMGUI)' started by DaniruKoresan, Sep 21, 2016.

  1. DaniruKoresan

    DaniruKoresan

    Joined:
    Apr 25, 2016
    Posts:
    2
    Okay so basically in our game the main menu for the game is displayed on an in-game laptop which has it's own mouse cursor exclusive to the laptop screen. When the player moves their real mouse it navigates this in-game cursor across the virtual laptop screen, relative to the player's mouse position.

    This is what I have so far:

    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;
    public class TestGUICursor : MonoBehaviour
    {
    public float fcursorspeed;
    public GameObject Cursor;
    Vector3 cursorPosition;

    void Update ()
    {
    cursorPosition = new Vector3(Input.GetAxis("Mouse X") * fcursorspeed, Input.GetAxis("Mouse Y") * fcursorspeed, 0f); Cursor.GetComponent<RectTransform>().localPosition = cursorPosition;
    }
    }

    This is the current behavior:

    https://gfycat.com/HotMelodicElephant

    Bandicam sucks and removed my mouse cursor, but basically when I move my mouse around the in-game cursor jitters around; and when my mouse is at rest the in-game cursor resets to it's original position.

    The project's main programmer suggests using the mouse position value from the Event System, but neither of us has an idea on how to access that value.

    (Also sorry about the S***ty formating, I have no idea where the code insert button is on this forum)
     
  2. MrMatthias

    MrMatthias

    Joined:
    Sep 18, 2012
    Posts:
    191
    use:
    Code (CSharp):
    1. Cursor.GetComponent<RectTransform>().localPosition += cursorPosition;
    GetAxis returns 0 when the user doesn't move the mouse
     
  3. gibbie_learnersedge

    gibbie_learnersedge

    Joined:
    Aug 11, 2016
    Posts:
    25