Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to limit mouse rotation when turning a gameobject

Discussion in 'Scripting' started by DarkEcho, Jan 28, 2015.

  1. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    233
    How do I limit the mouse speed when we are turning a object.
    As if your using the mouse to rotate a tank turret so the turret rotates at the same speed regardless of how fast or slow you move the mouse

    Heres my current scritpt
    Code (CSharp):
    1.         float turretLeftright = Input.GetAxis("MouseHorizontal") * turretTurnSpeed;
    2.  
    3.         turret.transform.Rotate (0, turretLeftright, 0);
     
  2. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    233
  3. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    I think if you do:
    turretLeftright * Time.deltaTime
     
  4. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    If you want to rotate the turret with a constant speed, you can try something
    like this :

    Code (CSharp):
    1. float rotFactor = 0f;
    2. if(0f != Input.GetAxis("Horizontal")) {
    3. rotFactor = Mathf.Sign( Input.GetAxis("Horizontal") );
    4. }
    5. float turretLeftright = rotFactor * turretTurnSpeed;
    6. turret.transform.Rotate(0, turretLeftright, 0);
     
  5. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    233
    Cheers buddy!