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

Controlling Animation via Mouse

Discussion in 'Scripting' started by mehware, Aug 25, 2008.

  1. mehware

    mehware

    Joined:
    Nov 19, 2007
    Posts:
    739
    Hi,

    I want to control my animation via mouse movement/drag. So if my mouse was towards the bottom of the screen it would be at 0, if my mouse was in the middle of the screen it would be at 0.5, and if my mouse was on the top of the screen it would be at 1.

    Here is some code so far:

    Code (csharp):
    1.  
    2. vertAxisMouse = Input.GetAxis("Mouse Y");
    3.     vertAxisMouse *= Time.deltaTime;
    4.     vertAxisNormalizedMouse = Mathf.Clamp(vertAxisMouse, 0.000, 1.000);
    5.     playerOneAnim["toss"].normalizedTime = vertAxisNormalizedMouse;
    6.     Debug.Log("Axis: " + vertAxisMouse + "Normalized: " + vertAxisNormalizedMouse);
    7.     playerOneAnim["toss"].time = vertAxisMouse;
    8.  
    Nothing happens though. Comments Welcome :)
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    One thing I noticed at first glance: don't use Time.deltaTime when dealing with mouse axes. That's already frame-rate independent to begin with, so multiplying by deltaTime makes it frame-rate dependent (somewhat ironically).

    Anyway, in this case, you don't want to use the Y mouse axis at all. From what you described, you'd want to use Input.mousePosition.y, and divide by Screen.height.

    --Eric
     
  3. mehware

    mehware

    Joined:
    Nov 19, 2007
    Posts:
    739
    Thanks, works perfectly now.