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

Question How Do I Do This

Discussion in 'Scripting' started by ShaunChad99, May 12, 2020.

  1. ShaunChad99

    ShaunChad99

    Joined:
    Apr 23, 2020
    Posts:
    26
    i can rotate round the X axis with my scroll wheel. but i want to hold down the mouse button and scroll to rotate round the Y how will i do this.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PickUpAndThrow : MonoBehaviour
    6. {
    7. //Rotate
    8.         public float rotateSpeed = 1;
    9.  
    10. void Update()
    11.         {
    12. //Rotate
    13.             if (Input.GetAxis("Mouse ScrollWheel") > 0)
    14.             {
    15.                 transform.Rotate(Vector3.left * rotateSpeed, Space.Self);
    16.             }
    17.             if (Input.GetAxis("Mouse ScrollWheel") < 0)
    18.             {
    19.                 transform.Rotate(Vector3.right * rotateSpeed, Space.Self);
    20.             }
    21.             if (Input.GetMouseButtonDown(1) && Input.GetAxis("Mouse ScrollWheel") > 0)
    22.             {
    23.                 transform.Rotate(Vector3.up * rotateSpeed, Space.Self);
    24.             }
    25.             if (Input.GetMouseButtonDown(1) && Input.GetAxis("Mouse ScrollWheel") < 0)
    26.             {
    27.                 transform.Rotate(Vector3.down * rotateSpeed, Space.Self);
    28.             }
    29. }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    "GetMouseButtonDown" will only be true for the first frame when you click the button. You probably want GetMouseButton() which is true for every frame as long as you hold it down.
     
  3. ShaunChad99

    ShaunChad99

    Joined:
    Apr 23, 2020
    Posts:
    26
    changed it to that but it still only rotates round the X axis
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Which button are you pressing on your mouse?
     
  5. ShaunChad99

    ShaunChad99

    Joined:
    Apr 23, 2020
    Posts:
    26
    left
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Well you should probably listen for that button in your script instead of the one you're currently listening for, which is the right button:
    Code (CSharp):
    1. // This is the left mouse button
    2. Input.GetMouseButton(0)
    3. // This is the right mouse button
    4. Input.GetMouseButton(1)
    See the example in the manual: https://docs.unity3d.com/ScriptReference/Input.GetMouseButton.html
     
  7. ShaunChad99

    ShaunChad99

    Joined:
    Apr 23, 2020
    Posts:
    26
    thank you look right over that silly me
     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    I will anticipate your next problem:

    When you hold the button down, it rotates on both axes. Double check your if conditions and you will see why.
     
  9. ShaunChad99

    ShaunChad99

    Joined:
    Apr 23, 2020
    Posts:
    26
    would using a return the best here or something else sorry im trying to learn
     
  10. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Imagine you are a computer and you are reading each statement in your program. Imagine the user is holding down the mouse button and scrolling the mouse wheel up:
    Code (CSharp):
    1. if (Input.GetAxis("Mouse ScrollWheel") > 0)
    2. {
    3.     transform.Rotate(Vector3.left * rotateSpeed, Space.Self);
    4. }
    Ok scroll wheel is greater than zero because the user is currently scrolling up, so I'll do this rotation. Let's move on...
    Code (CSharp):
    1. if (Input.GetAxis("Mouse ScrollWheel") < 0)
    2. {
    3.     transform.Rotate(Vector3.right * rotateSpeed, Space.Self);
    4. }
    Scroll wheel is going up, not down, I'll ignore this. Let's move on..
    Code (CSharp):
    1. if (Input.GetMouseButton(0) && Input.GetAxis("Mouse ScrollWheel") > 0)
    2. {
    3.     transform.Rotate(Vector3.up * rotateSpeed, Space.Self);
    4. }
    Ok the user is currently holding down mouse button 0 AND the scroll wheel IS greater than 0. I'll do this rotation and move on...
    Code (CSharp):
    1. if (Input.GetMouseButton(0) && Input.GetAxis("Mouse ScrollWheel") < 0)
    2. {
    3.     transform.Rotate(Vector3.down * rotateSpeed, Space.Self);
    4. }
    Ok yes the user is currently holding down mouse button 0, HOWEVER the scroll wheel is not less than 0. So I'll skip this part.

    Notice that the computer did both the first rotation and the third one. You need to change your "if" conditions so the computer ignores the first rotation if the user is holding down the mouse button.