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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Drag & Rotate around one Axis

Discussion in 'Scripting' started by Brokep, Oct 15, 2015.

  1. Brokep

    Brokep

    Joined:
    Jul 24, 2014
    Posts:
    15
    Hello there people, was messing around with some mouse drag rotate scripts and finally got a satisfying control over it. To be more specific I was looking into mousedrag and rotate, however while the script is okay and you can rotate the object I want to limit or set a min/max value on 2 of the 3 axis (xmin=0, xmax=0, zmin=0, zmax=0) and just drag-rotate the object on the Y axis only. I know this must be easy but I'm kinda newbie and couldn't figure something out. If anyone can help me out it would be really great.

    Thanks in advance for your help.

    Here is the script I last used (rotating on all 3 axis)

    Code (CSharp):
    1. using UnityEngine; using System.Collections;
    2. public class DragRotate : MonoBehaviour {
    3.    
    4.      public float mouseRotateSpeed = 20f;
    5.  
    6.  
    7.  
    8.      void OnMouseDrag()
    9.      {
    10.          transform.rotation = Quaternion.AngleAxis( -Input.GetAxis("Mouse X") * mouseRotateSpeed, Camera.main.transform.up) *
    11.                               Quaternion.AngleAxis(  Input.GetAxis("Mouse Y") * mouseRotateSpeed, Camera.main.transform.right) *
    12.                               transform.rotation;
    13.      }
    14. }
     
    Last edited: Oct 15, 2015
  2. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
  3. Brokep

    Brokep

    Joined:
    Jul 24, 2014
    Posts:
    15