Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    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:
    13
    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.     Transform target;
    9.  
    10.     Vector3 lastPostion;
    11.     //zooming
    12.     float zoomDistance;
    13.     float scrollSensitivity = 3f;
    14.  
    15.     public float minDistance = 3f;
    16.     public float maxDistance = 30f;
    17.  
    18.     //panning
    19.     float panSensitivity =0.05f;
    20.     //orbit
    21.  
    22.     float rotationSensitivity = 0.5f;
    23.  
    24.     void Awake()
    25.     {
    26.         rotationTarget = transform.parent;
    27.         target = rotationTarget.transform.parent;
    28.  
    29.         zoomDistance = Vector3.Distance (target.position,transform.position);
    30.  
    31.     }
    32.  
    33.     // Update is called once per frame
    34.     void Update () {
    35.        
    36.         transform.LookAt (target);
    37.  
    38.         if (Input.GetMouseButtonDown (1)) {
    39.  
    40.             lastPostion = Input.mousePosition;
    41.  
    42.         }
    43.         if (Input.GetMouseButtonDown (2)) {
    44.  
    45.             lastPostion = Input.mousePosition;
    46.  
    47.         }
    48.         Zoom();
    49.         Pan ();
    50.         Orbit ();
    51.     }
    52.     void Zoom() {
    53.  
    54.         float num = Input.GetAxis("Mouse ScrollWheel");
    55.         zoomDistance -= num * scrollSensitivity;
    56.  
    57.         zoomDistance = Mathf.Clamp(zoomDistance,minDistance,maxDistance);
    58.  
    59.         Vector3 pos = target.position;
    60.         pos -= transform.forward * zoomDistance;
    61.         transform.position = Vector3.Lerp(transform.position, pos, scrollSensitivity);
    62.         }
    63.  
    64.     void Pan() {
    65.  
    66.         if (Input.GetMouseButton (2)) {
    67.  
    68.             Vector3 delta = Input.mousePosition - lastPostion;
    69.             target.Translate (delta.x * panSensitivity, 0, delta.y * panSensitivity);
    70.             lastPostion = Input.mousePosition;
    71.         }
    72.  
    73.     }
    74.  
    75.     void Orbit() {
    76.         if (Input.GetMouseButton (1)) {
    77.  
    78.             Vector3 delta = Input.mousePosition - lastPostion;
    79.             float angleY = -delta.y * rotationSensitivity;
    80.             float angleX = delta.x * rotationSensitivity;
    81.             //x Axis
    82.             Vector3 angles = rotationTarget.transform.eulerAngles;
    83.  
    84.             angles.x += angleY;
    85.             angles.x = Mathf.Clamp (angles.x, 10f, 80f);
    86.  
    87.             rotationTarget.transform.eulerAngles = angles;
    88.             target.transform.RotateAround (target.position, Vector3.up, angleX);
    89.  
    90.             lastPostion = Input.mousePosition;
    91.  
    92.         }
    93.  
    94.     }
    95.  
    96. }
    97.  
    I created this script based on this video

    Enjoy.....
     

    Attached Files: