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. Dismiss Notice

Using mouse position for Blend Tree parameter?

Discussion in 'Editor & General Support' started by ApolloPM_legacy, Apr 19, 2014.

  1. ApolloPM_legacy

    ApolloPM_legacy

    Joined:
    Feb 3, 2014
    Posts:
    24
    Hello,

    I am currently trying to make my character aim up, aim neutral and aim down based on mouse position/movement but I do not want to base it on mouse speed/velocity.

    It is hard for me to explain the problem or question is so I will just show you a diagram of what I want to do.
    See attached image.

    $UnityQuestion1.png

    Here is a sample of the script

    Code (csharp):
    1. function Aim(){
    2.     var mousePos = Input.mousePosition.y;
    3.         if(Input.GetButton("Zoom")){  //Zoom is right click
    4.             animator.SetFloat("MouseAimY", mousePos); //This will put the number from mouse position (Y axis) coordinate into the parameter.
    5.        
    6.     }
    7. }
    Thanks guys!
     
  2. ApolloPM_legacy

    ApolloPM_legacy

    Joined:
    Feb 3, 2014
    Posts:
    24
    One idea came to mind. Should I get the camera's rotation instead?
     
  3. ApolloPM_legacy

    ApolloPM_legacy

    Joined:
    Feb 3, 2014
    Posts:
    24
    Awesome community guys.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    It tends to die a bit on the weekends - I know I pretty much only check for updates during the week.

    Anyway, you probably want to simply divide the mousePosition by Screen.height, and that'll give you a 0-to-1 value for the y.
     
  5. ApolloPM_legacy

    ApolloPM_legacy

    Joined:
    Feb 3, 2014
    Posts:
    24
    Hello,

    Thanks for the reply. I do not know what you mean by dividing mousePosition by Screen.height. Like where do I apply it on my script? What to replace or remove?

    Thank you.

    EDIT: I looked at the Unity docs, it did not gave me an example.
    http://docs.unity3d.com/Documentation/ScriptReference/Screen-height.html

    I am new to coding so I learn from using examples and reverse-engineering.
     
    Last edited: Apr 22, 2014
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    Code (csharp):
    1.  
    2.     var mousePos = Input.mousePosition.y / Screen.height;
    3.  
    Then adjust the thresholds in the Mecanim blend tree to match the numbers it now gives you, as desired.
     
  7. ApolloPM_legacy

    ApolloPM_legacy

    Joined:
    Feb 3, 2014
    Posts:
    24
    Thank you it worked perfectly!