How do I make an object rotate while pressing right mouse button?
Hello, this is my first time posting here. :) So right now I have a script that lets me rotate the object while holding down left mouse button. My question is - How do I make an object rotate while pressing right mouse button? My code right now:
float rotSpeed = 20;
void OnMouseDrag()
{
float rotX = Input.GetAxis("Mouse X") * rotSpeed * Mathf.Deg2Rad;
float rotY = Input.GetAxis("Mouse Y") * rotSpeed * Mathf.Deg2Rad;
transform.RotateAround(Vector3.up, -rotX);
transform.RotateAround(Vector3.right, rotY);
}
Can someone help me? I'm new at coding.
Answer by Hellium · Apr 23, 2018 at 07:52 AM
I advise you to use the EventSystem
instead of the plain MonoBehaviour
function OnMouseDrag
In the top menu, click on
GameObject > UI > EventSystem
Select your camera and attach the
PhysicsRaycaster
componentYour code should look like this:
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class DragUsingRightButton : MonoBehaviour, IBeginDragHandler, IDragHandler
{
public float rotSpeed = 5 ;
public void OnBeginDrag(PointerEventData data)
{
// Do nothing, but keep this function if you want `OnDrag` to be called
}
public void OnDrag(PointerEventData data)
{
if( data.button != PointerEventData.InputButton.Right)
return;
float rotX = data.delta.x * rotSpeed * Mathf.Deg2Rad;
float rotY = data.delta.y * rotSpeed * Mathf.Deg2Rad;
transform.RotateAround(Vector3.up, -rotX);
transform.RotateAround(Vector3.right, rotY);
}
}
ORIGINAL ANSWER
Have you simply tried to check whether the right mouse button is held down?
void OnMouseDrag()
{
if( !Input.GetMouseButton(1) )
return ;
float rotX = Input.GetAxis("Mouse X") * rotSpeed * Mathf.Deg2Rad;
float rotY = Input.GetAxis("Mouse Y") * rotSpeed * Mathf.Deg2Rad;
transform.RotateAround(Vector3.up, -rotX);
transform.RotateAround(Vector3.right, rotY);
}
Unfortunately this doesn't work :( This script doesn't let me rotate the object in any way. $$anonymous$$aybe it's because On$$anonymous$$ouseDrag works with the left mouse button?
I've added to my answer a complete new solution. Give it a try.
$$anonymous$$aybe you could help me with this problem too? https://answers.unity.com/questions/1498087/capture-screenshots-and-save-them-in-folder.html
Answer by PaulaOstupe · Apr 24, 2018 at 08:08 AM
The only thing I added for it to work was "public int RotSpeed = 5". This is the final code:
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class rotate_object : MonoBehaviour, IBeginDragHandler, IDragHandler
{
public int RotSpeed = 5;
public void OnBeginDrag(PointerEventData data)
{
// Do nothing, but keep this function if you want `OnDrag` to be called
}
public void OnDrag(PointerEventData data)
{
if (data.button != PointerEventData.InputButton.Right)
return;
float rotX = data.delta.x * RotSpeed * Mathf.Deg2Rad;
float rotY = data.delta.y * RotSpeed * Mathf.Deg2Rad;
transform.RotateAround(Vector3.up, -rotX);
transform.RotateAround(Vector3.right, rotY);
}
}