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.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PickUpAndThrow : MonoBehaviour
{
//Rotate
public float rotateSpeed = 1;
void Update()
{
//Rotate
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
transform.Rotate(Vector3.left * rotateSpeed, Space.Self);
}
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
transform.Rotate(Vector3.right * rotateSpeed, Space.Self);
}
if (Input.GetMouseButtonDown(1) && Input.GetAxis("Mouse ScrollWheel") > 0)
{
transform.Rotate(Vector3.up * rotateSpeed, Space.Self);
}
if (Input.GetMouseButtonDown(1) && Input.GetAxis("Mouse ScrollWheel") < 0)
{
transform.Rotate(Vector3.down * rotateSpeed, Space.Self);
}
}
“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.
changed it to that but it still only rotates round the X axis
Which button are you pressing on your mouse?
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:
// This is the left mouse button
Input.GetMouseButton(0)
// This is the right mouse button
Input.GetMouseButton(1)
See the example in the manual: Unity - Scripting API: Input.GetMouseButton
PraetorBlue:
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:
// This is the left mouse button
Input.GetMouseButton(0)
// This is the right mouse button
Input.GetMouseButton(1)
See the example in the manual: https://docs.unity3d.com/ScriptReference/Input.GetMouseButton.html
thank you look right over that silly me
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.
PraetorBlue:
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.
would using a return the best here or something else sorry im trying to learn
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:
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
transform.Rotate(Vector3.left * rotateSpeed, Space.Self);
}
Ok scroll wheel is greater than zero because the user is currently scrolling up, so I’ll do this rotation. Let’s move on…
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
transform.Rotate(Vector3.right * rotateSpeed, Space.Self);
}
Scroll wheel is going up, not down, I’ll ignore this. Let’s move on…
if (Input.GetMouseButton(0) && Input.GetAxis("Mouse ScrollWheel") > 0)
{
transform.Rotate(Vector3.up * rotateSpeed, Space.Self);
}
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…
if (Input.GetMouseButton(0) && Input.GetAxis("Mouse ScrollWheel") < 0)
{
transform.Rotate(Vector3.down * rotateSpeed, Space.Self);
}
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.