Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

How to control X and Y axis for 3D object Rotation in Input.GetMouseButtonDown(0) ?

Discussion in 'Physics' started by syedimran199696, Jul 7, 2018.

  1. syedimran199696

    syedimran199696

    Joined:
    Apr 4, 2018
    Posts:
    12
    Hello Everyone,

    Can you help me for in this struggle. I want to know "How to control X and Y axis for 3D Object Rotation with Input.GetMouseButtonDown(0) ? ". I want a same script and functionality like as attached video. Can you give me any suggestion for this query. Please share your script to me. I attached on below reference 3D Rotation Video link. Share your comments about it.

    This is my project screenshot :

    Screenshot (5).png

    My reference video link :




    Advance in Thanks,
    Syed Imran S.A
     
  2. sweetan

    sweetan

    Joined:
    Nov 21, 2017
    Posts:
    17
    Hi Syed Imran,
    I hope I will solve your problem. I attached my script below and screenshot below try this. If it works let me know.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraController : MonoBehaviour {
    6.  
    7.     Transform rotationTarget;
    8.  
    9.     Vector3 lastPostion;
    10.     //zooming
    11.     float zoomDistance;
    12.     float scrollSensitivity = 3f;
    13.  
    14.     public float minDistance = 3f;
    15.     public float maxDistance = 30f;
    16.  
    17.     //panning
    18.     float panSensitivity =0.05f;
    19.     //orbit
    20.  
    21.     float rotationSensitivity = 0.5f;
    22.  
    23.     void Awake()
    24.     {
    25.         rotationTarget = transform.parent;
    26.         target = rotationTarget.transform.parent;
    27.  
    28.         zoomDistance = Vector3.Distance (target.position,transform.position);
    29.  
    30.     }
    31.  
    32.     // Update is called once per frame
    33.     void Update () {
    34.        
    35.         transform.LookAt (target);
    36.  
    37.         if (Input.GetMouseButtonDown (1)) {
    38.  
    39.             lastPostion = Input.mousePosition;
    40.  
    41.         }
    42.         if (Input.GetMouseButtonDown (2)) {
    43.  
    44.             lastPostion = Input.mousePosition;
    45.  
    46.         }
    47.         Zoom();
    48.         Pan ();
    49.         Orbit ();
    50.     }
    51.     void Zoom() {
    52.  
    53.         float num = Input.GetAxis("Mouse ScrollWheel");
    54.         zoomDistance -= num * scrollSensitivity;
    55.  
    56.         zoomDistance = Mathf.Clamp(zoomDistance,minDistance,maxDistance);
    57.  
    58.         Vector3 pos = target.position;
    59.         pos -= transform.forward * zoomDistance;
    60.         transform.position = Vector3.Lerp(transform.position, pos, scrollSensitivity);
    61.         }
    62.  
    63.     void Pan() {
    64.  
    65.         if (Input.GetMouseButton (2)) {
    66.  
    67.             Vector3 delta = Input.mousePosition - lastPostion;
    68.             target.Translate (delta.x * panSensitivity, 0, delta.y * panSensitivity);
    69.             lastPostion = Input.mousePosition;
    70.         }
    71.  
    72.     }
    73.  
    74.     void Orbit() {
    75.         if (Input.GetMouseButton (1)) {
    76.  
    77.             Vector3 delta = Input.mousePosition - lastPostion;
    78.             float angleY = -delta.y * rotationSensitivity;
    79.             float angleX = delta.x * rotationSensitivity;
    80.             //x Axis
    81.             Vector3 angles = rotationTarget.transform.eulerAngles;
    82.  
    83.             angles.x += angleY;
    84.             angles.x = Mathf.Clamp (angles.x, 10f, 80f);
    85.  
    86.             rotationTarget.transform.eulerAngles = angles;
    87.             target.transform.RotateAround (target.position, Vector3.up, angleX);
    88.  
    89.             lastPostion = Input.mousePosition;
    90.  
    91.         }
    92.  
    93.     }
    94.  
    95. }
    96.  
    I created this script based on this video

    Enjoy.....
     

    Attached Files: