Search Unity

I want to rotate Object around a Camera on mouse drag and also compatible with fingers on phone

Discussion in 'Scripting' started by mmtukur, Aug 13, 2022.

?

Please check this [MEDIA=gfycat]highlevelfickleequestrian[/MEDIA], I want to make this: [MEDIA=gfyca

  1. I want to rotate Object around a Camera on mouse drag and also compatible with fingers on phone

    1 vote(s)
    100.0%
  2. I want to rotate Object around a Camera on mouse drag and also compatible with fingers on phone

    0 vote(s)
    0.0%
  1. mmtukur

    mmtukur

    Joined:
    Aug 1, 2022
    Posts:
    1
    Please check this
    , I want to make this:
    exactly as the first one..! I want to rotate Object around a Camera on mouse drag and also compatible with fingers on phone and also I want to limit the ZoomIn/Out..
    I use this code below but the zoom has no limit and its not smooth.
    Please help me..!
    Thanks

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6.  
    7. public class MouseOrbit : MonoBehaviour {
    8.     public Transform target;
    9.     public float maxOffsetDistance = 2f;
    10.     public float orbitSpeed = 15f;
    11.     public float panSpeed = .5f;
    12.     public float zoomSpeed = 3f;
    13.     private Vector3 targetOffset = Vector3.zero;
    14.     private Vector3 targetPosition;
    15.    
    16.  
    17.  
    18.     // Use this for initialization
    19.     void Start() {
    20.         if (target != null) transform.LookAt(target);
    21.     }
    22.  
    23.  
    24.  
    25.     void Update() {
    26.         targetPosition = target.position + targetOffset;
    27.  
    28.  
    29.         if (target != null) {
    30.             targetPosition = target.position + targetOffset;
    31.  
    32.             // Left Mouse to Orbit
    33.             if (Input.GetMouseButton(0)) {
    34.                 transform.RotateAround(targetPosition, Vector3.up, Input.GetAxis("Mouse X") * orbitSpeed);
    35.                 float pitchAngle = Vector3.Angle(Vector3.up, transform.forward);
    36.                 float pitchDelta = -Input.GetAxis("Mouse Y") * orbitSpeed;
    37.                 float newAngle = Mathf.Clamp(pitchAngle + pitchDelta, 0f, 180f);
    38.                 pitchDelta = newAngle - pitchAngle;
    39.                 transform.RotateAround(targetPosition, transform.right, pitchDelta);
    40.             }
    41.             // Right Mouse To Pan
    42.             if (Input.GetMouseButton(1)) {
    43.                 Vector3 offset = transform.right * -Input.GetAxis("Mouse X") * panSpeed + transform.up * -Input.GetAxis("Mouse Y") * panSpeed;
    44.                 Vector3 newTargetOffset = Vector3.ClampMagnitude(targetOffset + offset, maxOffsetDistance);
    45.                 transform.position += newTargetOffset - targetOffset;
    46.                 targetOffset = newTargetOffset;
    47.             }
    48.  
    49.  
    50.             // Scroll to Zoom
    51.             transform.position += transform.forward * Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;
    52.  
    53.         }
    54.     }
    55. }// CLASS ```