P
P
polyakovyevgeniy2017-03-28 17:44:09
Unity
polyakovyevgeniy, 2017-03-28 17:44:09

Camera rotation around an object with a swipe in Unity3D?

Please tell me how you can make the camera rotate around the object with your finger just in a circle and work like scrolling. That is, the swipe did, and she herself rotates a little more by inertia.
I have a sphere. There is a chamber and a cube. The camera is a child of the cube. And the sphere is the object in the center of which there should be a cube and rotate along its axis. I have to swipe across the screen, and the cube should begin to rotate in one direction or another with inertia. Are there examples of how this can be done?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Daniil Basmanov, 2017-03-28
@polyakovyevgeniy

My toolkit has a controller for the camera that does what you want, but based on the UI to make it easier to limit the propagation of click events. You can see it in action here . If you don’t use canvas, then you can transfer the code from OnDrag to some update and consider the delta as hands.

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace ProceduralToolkit.Examples
{
    /// <summary>
    /// Simple camera controller
    /// </summary>
    [RequireComponent(typeof (Image))]
    public class CameraRotator : UIBehaviour, IDragHandler
    {
        public Transform cameraTransform;
        public Transform target;
        [Header("Position")]
        public float distanceMin = 10;
        public float distanceMax = 30;
        public float yOffset = 0;
        public float scrollSensitivity = 1000;
        public float scrollSmoothing = 10;
        [Header("Rotation")]
        public float tiltMin = -85;
        public float tiltMax = 85;
        public float rotationSensitivity = 0.5f;
        public float rotationSpeed = 20;

        private float distance;
        private float scrollDistance;
        private float velocity;
        private float lookAngle;
        private float tiltAngle;
        private Quaternion rotation;

        protected override void Awake()
        {
            base.Awake();
            tiltAngle = (tiltMin + tiltMax)/2;
            distance = scrollDistance = (distanceMax + distanceMin)/2;

            if (cameraTransform == null || target == null) return;

            cameraTransform.rotation = rotation = Quaternion.Euler(tiltAngle, lookAngle, 0);
            cameraTransform.position = CalculateCameraPosition();
        }

        private void LateUpdate()
        {
            if (cameraTransform == null || target == null) return;

            if (cameraTransform.rotation != rotation)
            {
                cameraTransform.rotation = Quaternion.Lerp(cameraTransform.rotation, rotation,
                    Time.deltaTime*rotationSpeed);
            }

            float scroll = Input.GetAxis("Mouse ScrollWheel");
            if (scroll != 0)
            {
                scrollDistance -= scroll*Time.deltaTime*scrollSensitivity;
                scrollDistance = Mathf.Clamp(scrollDistance, distanceMin, distanceMax);
            }

            if (distance != scrollDistance)
            {
                distance = Mathf.SmoothDamp(distance, scrollDistance, ref velocity, Time.deltaTime*scrollSmoothing);
            }

            cameraTransform.position = CalculateCameraPosition();
        }

        public void OnDrag(PointerEventData eventData)
        {
            if (cameraTransform == null || target == null) return;

            lookAngle += eventData.delta.x*rotationSensitivity;
            tiltAngle -= eventData.delta.y*rotationSensitivity;
            tiltAngle = Mathf.Clamp(tiltAngle, tiltMin, tiltMax);
            rotation = Quaternion.Euler(tiltAngle, lookAngle, 0);
        }

        private Vector3 CalculateCameraPosition()
        {
            return target.position + cameraTransform.rotation*(Vector3.back*distance) + Vector3.up*yOffset;
        }
    }
}

D
Denis Gaydak, 2017-03-28
@MrMureno

I really hope that you will also find about svaip in Google, everything seems to be simple there.
But by inertia, I'll tell you the principle.
In fact, there is a delta in the wheelbarrow (and in the mouse cursor) https://docs.unity3d.com/ScriptReference/Touch-del...
In fact, how much it has shifted per frame (if you read on the update)
So you can just delta, you can average for several frames (as you like best) - remember.
This memorized delta will be a measure of inertia) And in fact, you just need to continue on the update to apply such a delta to the rotation and slowly reduce it (it’s trite in the same update to multiply by something smaller and close to unity, some 0.97)
The multiplier itself will be a measure of inertia)

//////////
                      Vector2 position = UnifiedInput.position();
                      dLong = -position.x * longitudeSensivity;
                      dLat = position.y * latitudeSensivity;

                      velocity += new Vector2(dLat, dLong);
             
                  //      Also implement tap controls

                  if (velocity.sqrMagnitude > boundary * boundary)
                  {
                      float dt = Time.deltaTime;

                      Vector2 prevVelocity = velocity;
                      velocity -= dt * damping * velocity;
                      if (Vector2.Dot(velocity, prevVelocity) <= 0)
                          velocity = Vector2.zero;

                      latitude += dt * velocity.x;
                      longitude += dt * velocity.y;

                      latitude = Mathf.Clamp(latitude, 0 + boundary, Mathf.PI - boundary);
                      longitude = Mathf.Repeat(longitude, 2 * Mathf.PI);

                      relativePosition = SphericalToCartesian(longitude, latitude, orbitRadius);
                  }
                
                  this.transform.position = this.realTarget.transform.position + this.relativePosition;
                	this.transform.LookAt(this.realTarget.transform);

///////
private Vector3 SphericalToCartesian(float longitude, float latitude, float radius)
    {
        float x = radius * Mathf.Sin(latitude) * Mathf.Cos(longitude);
        float y = radius * Mathf.Cos(latitude);
        float z = radius * Mathf.Sin(latitude) * Mathf.Sin(longitude);
        return new Vector3(x, y, z);
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question