Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Is it possible to move the mouse cursor left and right using the scroll wheel?

Discussion in 'Input System' started by BunnysMeow, May 25, 2024.

  1. BunnysMeow

    BunnysMeow

    Joined:
    Mar 27, 2024
    Posts:
    2
    Hello. I downloaded a bubble shooter game off the internet, and am now trying to implement a feature in which the mouse cursor moves left and right, as the 'shooter' moves depending on the cursor location. Is there a way to do this? I've tried multiple things but ultimately, nothing worked.

    Code (CSharp):
    1. public void Update()
    2.     {
    3.         Vector3 mouse = Input.mousePosition;
    4.         if (Input.GetAxis("MouseScrollWheel") > 0)
    5.         {
    6.             [DllImport("user32.dll")]
    7.             public static extern bool SetCursorPos(lookDirection +1, int Y);
    8.         }
    9.         else if (Input.GetAxis("Mouse ScrollWheel") < 0)
    10.         {
    11.             [DllImport("user32.dll")]
    12.             public static extern bool SetCursorPos(lookDirection -1, int Y);
    13.         }
    14.  
    15.         lookDirection = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
    16.         lookAngle = Mathf.Atan2(lookDirection.y, lookDirection.x) * Mathf.Rad2Deg;
    17.         gunSprite.rotation = Quaternion.Euler(0f, 0f, lookAngle - 90f);
    18.     }
    This is currently what I have ended up with, but it still doesn't work. What could I do to fix this?


    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3.  
    4. public class Shooter : MonoBehaviour
    5. {
    6.     public Transform gunSprite;
    7.     public bool canShoot;
    8.     public float speed = 6f;
    9.     public float rotation;
    10.     float rotationSpeed = 100f; // Adjust this value as needed
    11.  
    12.     public Transform nextBubblePosition;
    13.     public GameObject currentBubble;
    14.     public GameObject nextBubble;
    15.  
    16.     private Vector2 lookDirection;
    17.     private float lookAngle;
    18.     public bool isSwaping = false;
    19.     public float time = 0.02f;
    20.     public float scale = 1f;
    21.  
    22.     public void Update()
    23.     {
    24.         Vector3 mouse = Input.mousePosition;
    25.         if (Input.GetAxis("MouseScrollWheel") > 0)
    26.         {
    27.             [DllImport("user32.dll")]
    28.             public static extern bool SetCursorPos(lookDirection +1, int Y);
    29.         }
    30.         else if (Input.GetAxis("Mouse ScrollWheel") < 0)
    31.         {
    32.             [DllImport("user32.dll")]
    33.             public static extern bool SetCursorPos(lookDirection -1, int Y);
    34.         }
    35.  
    36.         lookDirection = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
    37.         lookAngle = Mathf.Atan2(lookDirection.y, lookDirection.x) * Mathf.Rad2Deg;
    38.         gunSprite.rotation = Quaternion.Euler(0f, 0f, lookAngle - 90f);
    39.     }
    40.  
    41.         if(isSwaping)
    42.         {
    43.             if(Vector2.Distance(currentBubble.transform.position, nextBubblePosition.position) <= 0.2f
    44.                 && Vector2.Distance(nextBubble.transform.position, transform.position) <= 0.2f)
    45.             {
    46.                 nextBubble.transform.position = transform.position;
    47.                 currentBubble.transform.position = nextBubblePosition.position;
    48.  
    49.                 currentBubble.GetComponent<Collider2D>().enabled = true;
    50.                 nextBubble.GetComponent<Collider2D>().enabled = true;
    51.  
    52.                 isSwaping = false;
    53.  
    54.                 GameObject reference = currentBubble;
    55.                 currentBubble = nextBubble;
    56.                 nextBubble = reference;
    57.             }
    58.  
    59.             nextBubble.transform.position = Vector2.Lerp(nextBubble.transform.position, transform.position, time);
    60.             currentBubble.transform.position = Vector2.Lerp(currentBubble.transform.position, nextBubblePosition.position, time);
    61.         }
    62.     }
    63.  
    64.     public void Shoot()
    65.     {
    66.         transform.rotation = Quaternion.Euler(0f, 0f, lookAngle - 90f);
    67.         currentBubble.transform.rotation = transform.rotation;
    68.         currentBubble.GetComponent<Rigidbody2D>().AddForce(currentBubble.transform.up * speed, ForceMode2D.Impulse);
    69.         currentBubble = null;
    70.     }
    71.  
    72.     [ContextMenu("SwapBubbles")]
    73.     public void SwapBubbles()
    74.     {
    75.         currentBubble.GetComponent<Collider2D>().enabled = false;
    76.         nextBubble.GetComponent<Collider2D>().enabled = false;
    77.         isSwaping = true;
    78.     }
    79.  
    80.     [ContextMenu("CreateNextBubble")]
    81.     public void CreateNextBubble()
    82.     {
    83.         List<GameObject> bubblesInScene = LevelManager.instance.bubblesInScene;
    84.         List<string> colors = LevelManager.instance.colorsInScene;
    85.  
    86.         if (nextBubble == null)
    87.         {
    88.             nextBubble = InstantiateNewBubble(bubblesInScene);
    89.         }
    90.         else
    91.         {
    92.             if(!colors.Contains(nextBubble.GetComponent<Bubble>().bubbleColor.ToString()))
    93.             {
    94.                 Destroy(nextBubble);
    95.                 nextBubble = InstantiateNewBubble(bubblesInScene);
    96.             }
    97.         }
    98.  
    99.         if(currentBubble == null)
    100.         {
    101.             currentBubble = nextBubble;
    102.             currentBubble.transform.position = new Vector2(transform.position.x, transform.position.y);
    103.             nextBubble = InstantiateNewBubble(bubblesInScene);
    104.         }
    105.     }
    106.  
    107.     private GameObject InstantiateNewBubble(List<GameObject> bubblesInScene)
    108.     {
    109.         GameObject newBubble = Instantiate(bubblesInScene[(int)(Random.Range(0, bubblesInScene.Count * 1000000f) / 1000000f)]);
    110.         newBubble.transform.position = new Vector2(nextBubblePosition.position.x, nextBubblePosition.position.y);
    111.         newBubble.GetComponent<Bubble>().isFixed = false;
    112.         Rigidbody2D rb2d = newBubble.AddComponent(typeof(Rigidbody2D)) as Rigidbody2D;
    113.         rb2d.gravityScale = 0f;
    114.  
    115.         return newBubble;
    116.     }
    117. }
    118.  
    This is also the full code in my script if needed!
     
  2. skrizf

    skrizf

    Joined:
    Dec 29, 2020
    Posts:
    6
    If you send image of the game's interface, I will try to help because I don't understand exactly what you want.
     
    samana1407 likes this.
  3. BunnysMeow

    BunnysMeow

    Joined:
    Mar 27, 2024
    Posts:
    2
    upload_2024-5-26_21-54-48.png

    This is what the game looks like.