SoFunction
Updated on 2025-03-07

Unity3D realizes object rotation, scaling and moving effects

This article shares the specific code for Unity3D to implement object rotation, scaling and movement for your reference. The specific content is as follows

Since the project runs on Android, it is more troublesome to use plug-ins. You can modify the trigger conditions, and you can do it without plug-ins.

1. Download the FingerGestures plug-in Download addressClick to open the link

2. Import the plug-in and create a scene. Drag and drop the preset Finger Gestures Initializer into the Hierarchy view

3. Add the script and drag it to the camera. Create a block and drag it onto the script target property.

using UnityEngine;
using ;
 
public class ObjectControl : MonoBehaviour
{
 public Transform target;
 public float yawSensitivity = 80.0f;
 public float pitchSensitivity = 160.0f;
 public bool clampPitchAngle = true;
 public float pinchZoomSensitivity = 0.5f;//Scaling speed public float smoothZoomSpeed = 10.0f;
 public float smoothOrbitSpeed = 20.0f;
 public float distance = 0;
 
 float yaw = 0;
 float pitch = 0;
 float idealYaw = 0;
 float idealPitch = 0;
 float fChangeScale = 0;
 float fChangeideal = 0;
 public Transform[] movementP;
 
 /// <summary>
 /// Control mode enumeration /// </summary>
 public enum ControlModel
 {
 Zoom, Rotate, Translate
 }
 
 public ControlModel controlModel = ;
 
 //Vector3 position=new Vector3();
 public bool bArrive = false;//Whether the mouse reaches the boundary area of ​​the part box // Is the translation method based on the drag distance of the mouse or is it directly set to the mouse position? public bool ifDragMove = false;
 //The translation method is: the speed of evaluation when dragging the mouse according to the distance public float moveSpeed = 1.0f;
 //It is enough to draw buttons (zoom, rotate, and pan) public bool ifDrawBtn = true;
 //Change the zoom method to: change the camera range public bool zoomCamera = false;
 //zoomCamera = true , the minimum range value of the camera public float minZoom = 0f;
 //zoomCamera = true , the maximum range value of the camera public float maxZoom = 179f;
 //Platform object public Transform moveTarget;
 //The initial position of the translation object Vector3 moveTargetPos;
 //The direct parent object of the model public Transform parentModel;
 Vector3 parentModelPos;
 
 void Start()
 {
 zoomCamera = true;
 }
 
 void OnEnable()
 {
 
  += FingerGestures_OnDragMove;
  += FingerGestures_OnPinchMove;
  += OnFingerDragEnd;
 
 }
 
 void OnDisable()
 {
  -= FingerGestures_OnDragMove;
  -= FingerGestures_OnPinchMove;
  -= OnFingerDragEnd;
 }
 
 public void setRotation()
 {
 Vector3 angles = ;
 yaw = idealYaw = ;
 pitch = idealPitch = ;
 }
 
 void FingerGestures_OnDragMove(Vector2 fingerPos, Vector2 delta)
 {
 onDrag = true;
 try
 {
   = false;
 }
 catch
 {
   = false;
 }
 if (controlModel ==  && !bArrive)
 {
  idealYaw -=  * yawSensitivity * 0.02f;
  idealPitch +=  * pitchSensitivity * 0.02f;
  len = delta;
  if (target) (new Vector3(, -, 0), );
 }
 if (controlModel ==  && !bArrive)
 {
  if (ifDragMove)
  {
  if (moveTarget == null)
  {
    = new Vector3( +  * moveSpeed,  +  * moveSpeed, );// GetWorldPos( fingerPos );
  }
  else
  {
    = new Vector3( +  * moveSpeed,  +  * moveSpeed, );
  }
  }
  else
  {
  if (moveTarget == null)
  {
    = GetWorldPos(fingerPos);
  }
  else
  {
    = GetWorldPos(fingerPos);
  }
  }
 }
 
 }
 
 void FingerGestures_OnPinchMove(Vector2 fingerPos1, Vector2 fingerPos2, float delta)
 {
 
 if (controlModel ==  && !bArrive)
 {
  if (zoomCamera)
  {
  float fZoom =  - delta * pinchZoomSensitivity * 800 * ;
  fZoom = (fZoom, maxZoom);
  fZoom = (fZoom, minZoom);
   = (, fZoom,  * smoothZoomSpeed);
  //  =  - fZoom * ;
  }
  else
  {
 
  fChangeScale =  + delta * pinchZoomSensitivity;
 
  Vector3 vc = new Vector3(fChangeScale, fChangeScale, fChangeScale);
  }
 }
 }
 //Sliding ends void OnFingerDragEnd(int fingerIndex, Vector2 fingerPos)
 {
  = true;
 
 onDrag = false;
 }
 
 
 //Convert Unity screen coordinates to 3D coordinates Vector3 GetWorldPos(Vector2 screenPos)
 {
 // Camera mainCamera = ;
 Camera mainCamera = ("MainCamera").GetComponent<Camera>();
 if (!)
 {
  mainCamera = ("CameraOne").GetComponent<Camera>();
 }
 return (new Vector3(, , ( - )));
 }
 
 void Apply()
 {
 if (controlModel ==  && !bArrive)
 {
  yaw = (yaw, idealYaw,  * smoothOrbitSpeed);
  pitch = (pitch, idealPitch,  * smoothOrbitSpeed);
 }
 }
 bool onDrag;
 Vector2 len;
 
 void LateUpdate()
 {
 if ((1) || (0))
 {
   = true;
 }
 Apply();
 }
 
 static float ClampAngle(float angle, float min, float max)
 {
 if (angle < -360)
  angle += 360;
 
 if (angle > 360)
  angle -= 360;
 
 return (angle, min, max);
 }
 
 void Update()
 {
 ///Free switching 
 if ((0))
 {
 
  controlModel = ;
 }
 
 if ((1))
 {
 
  controlModel = ;
 }
 
 if (("Mouse ScrollWheel") != 0)
 {
  controlModel = ;
 }
 
 }
 
 /// <summary>
 /// Reset /// </summary>
 public void ResetValue()
 {
 if (moveTarget != null)
 {
   = moveTargetPos;
 }
 if (parentModel != null)
 {
   = parentModelPos;
 }
 yaw = 0;
 pitch = 0;
 idealYaw = 0;
 idealPitch = 0;
 }
 
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.