Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Detecting speed of the mouse move.

Discussion in 'Editor & General Support' started by Passero, Nov 15, 2009.

  1. Passero

    Passero

    Joined:
    Oct 7, 2009
    Posts:
    63
    In my game i want the user give the ability to hit objects with the mousecuror. The strength of the hit should be depening on the speed of the mouse.

    So if the user moves the mouse very fast to the object, it should get a bigger force. When the user moves the mouse slowly to the object, the force should also be little.

    I know how i can add force to an object but i don't know how to detect the speed of the mouse
     
  2. bapetey

    bapetey

    Joined:
    Nov 16, 2009
    Posts:
    10
    Not sure if this is exactly what you're looking for, but hopefully it will get you started.

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class MouseSpeed : MonoBehaviour
    4. {
    5.     // store how much the mouse has moved since the last frame
    6.     public Vector3 mouseDelta = Vector3.zero;
    7.    
    8.     private Vector3 lastMousePosition = Vector3.zero;
    9.    
    10.     void Update()
    11.     {
    12.         mouseDelta = Input.mousePosition - lastMousePosition;
    13.  
    14.         lastMousePosition = Input.mousePosition;
    15.     }
    16. }
     
    DonPuno likes this.
  3. burnumd

    burnumd

    Joined:
    May 27, 2008
    Posts:
    367
    There's a very easy way to get this information:
    Code (csharp):
    1.  
    2. Input.GetAxis("Mouse X");
    3.  
    Code (csharp):
    1.  
    2. Input.GetAxis("Mouse Y");
    3.  
    Give you a value where "mouse delta is multiplied by the axis sensitivity." Input.GetAxis and Input.GetButton/GetButtonDown/GetButtonUp are the preferred means of getting Input in Unity scripting. If you need to know the name of the button/axis you want to read (or want to define your own), check Edit->Project Settings->Input.
     
    DonPuno likes this.
  4. David_29

    David_29

    Joined:
    Jan 22, 2014
    Posts:
    12
    Is it measured by pixel resolution to check for drag speed power?
     
    fegabe likes this.
  5. MDragon

    MDragon

    Joined:
    Dec 26, 2013
    Posts:
    329
    I believe it's in World Coordinates, but you can always convert between World and Screen Coordinates (ie Camera.main.ScreenToWorldPoint and the opposite one, although I primarily use ScreenToWorldPoint: http://docs.unity3d.com/Documentation/ScriptReference/Camera.html ).

    You would just save the position at when the mouse button was first clicked, and then either use a timer or get the final position when the mouse button is released (probably the former). Then subtract the two (in World coordinates probably- your good old simple XYZ rather than pixels) and you'd get your "drag speed power."

    And just in case I wasn't clear (and I'm pretty sure I wasn't), I mentioned using ScreenToWorldPoint() and WorldToScreenPoint() in order to get values in relation to the size of the screen- so that it works across all sizes of screens and screen ratios. There are a few ways to use the two to get what you need, but that's essentially it.
     
    fegabe likes this.
  6. DonPuno

    DonPuno

    Joined:
    Dec 22, 2020
    Posts:
    57
    It helped! Thanks for the valuable post!