0

I want to rotate a GameObject through mouse movement between -20° and +20°.

I know that there's a possibility to grab the smoothed Input through Input.GetAxis("Horizontal"); and Input.GetAxis("Vertical"); which returns float values between -1 and 1

It would be nice if there would exist something like Input.GetMouseAxis("Horizontal"); or Input.GetMouseAxis("Horizontal");.

But I the output shouldn't be influenced by keyboard. I'm sorry that I don't have much experience with Unity and its API.

5
  • Have you tried Input.GetAxisRaw() docs.unity3d.com/ScriptReference/Input.GetAxisRaw.html Commented Jun 8, 2015 at 16:18
  • You could also try Input.MousePosition() docs.unity3d.com/ScriptReference/Input-mousePosition.html Commented Jun 8, 2015 at 16:20
  • is the mouse in unity "captured" in screen resolution or how should I scale the mouse Position to a value between -1 and 1 ? Commented Jun 8, 2015 at 16:32
  • I'm fairly certain that it is, as you say, captured inside the resolution, try Debug.Log(Input.MousePosition()); to see what comes up, same with getaxisraw. (Sorry if you already new to do this). Commented Jun 8, 2015 at 16:36
  • >the raw's don't change on mouse move >the mouseposition seems to be a bit strange-in the upper left corner is [0,480] and not like in winforms [0,0] :/ , but the values could get negative in not-fullscreenmode Commented Jun 8, 2015 at 16:42

1 Answer 1

1

is the mouse in unity "captured" in screen resolution or how should I scale the mouse Position to a value between -1 and 1 ?

In this case the "normalizedSpeed" will be your target

public float mouseDistance;
public Vector2 mouseDown = -Vector2.one;

public float normalizedSpeed;

public void Update()
{
   if(Input.GetMouseButtonDown(0))
      mouseDown = Input.MousePosition();

   if(Input.GetMouseButtonUp(0))
      mouseDown = -Vector2.one;

   if(Input.GetMouseButton(0))
      normalizedSpeed = mouseDown != -Vector2.one ?
      Mathf.Clamp((mouseDown - Input.MousePosition()).sqrMagnitude, 0, (mouseDistance * mouseDistance)) / (mouseDistance * mouseDistance)
      : 0;
}

i use sqrMagnitude instead of distance because the sqr root call in distance() takes alot of memory, so it is faster to compare the squared distances vs each other

Also keep in mind im wirting this as a pseudo-code so the overall idea is what im going for, im leaving you syntactically responsible for the implementation ;)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.