Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by PaulaOstupe · Apr 22, 2018 at 07:50 PM · rotationrotate objectmouseclickright click

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Hellium · Apr 23, 2018 at 07:52 AM

I advise you to use the EventSystem instead of the plain MonoBehaviour function OnMouseDrag

  1. In the top menu, click on GameObject > UI > EventSystem

  2. Select your camera and attach the PhysicsRaycaster component

  3. Your 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);
  }






Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PaulaOstupe · Apr 23, 2018 at 10:14 AM 0
Share

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?

avatar image Hellium PaulaOstupe · Apr 23, 2018 at 03:43 PM -1
Share

I've added to my answer a complete new solution. Give it a try.

avatar image PaulaOstupe Hellium · Apr 24, 2018 at 01:43 PM 0
Share

$$anonymous$$aybe you could help me with this problem too? https://answers.unity.com/questions/1498087/capture-screenshots-and-save-them-in-folder.html

avatar image PaulaOstupe · Apr 24, 2018 at 08:05 AM 0
Share

Thank you!!!!

avatar image
0

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);
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

167 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotating around another object without parent/child relation?? 0 Answers

how do I rotate an object without affecting later rotation? 0 Answers

Simple Rotate Script won't work! 2 Answers

Making Weapon look at middle of the screen 0 Answers

How to rotate a cube in unity? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges