Search Unity

  1. Unity 2019.1 beta is now available.
    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. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. On February 28th the Feedback website will shut down and be redirected to the Unity forums. See the full post for more information.
    Dismiss Notice
  5. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  6. Unity 2018.3 is now released.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    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: