Topic: Smooth Rotate with Camera Rotate to Compass

First off ...I want to thank you for all the help and support I have received since my purchase of your Online Maps plug-in.  I've almost got my setup to the place I need it to be to function for my needs.  Few things I still have not worked out yet.

1.  Get camera to rotate smoothly - After enabing the rotate camera by compass script it seems to rotate in clicks or degrees rather then smooth as one would desire.  After digging into the script I don't see any parameters to up the rotate amount to smooth things out.  Is it possible to achieve this effect?

2.  Off-Set the marker orbit script - I like how this script focuses on to the location service enabled Prefab but what can I do to off center it while still having it rotate.  I'd like it to be in the bottom third of the screen on a mobile device.

3.   Alternatively from using the compass to rotate the camera... what is the best method of rotating the camera by using touch screen methods.  I would use this instead of the compass.  Just want to try all the functions and determine which is best for my application. 

Thank you!
Nate

Re: Smooth Rotate with Camera Rotate to Compass

Also, I'll note I just purchased and enabled Easy-Touch into my build if this resolves the rotate issue.

Re: Smooth Rotate with Camera Rotate to Compass

Hello.

1. Use Mathf.Lerp to smooth the rotation of the camera.
https://docs.unity3d.com/ScriptReferenc … .Lerp.html
Something like:

using UnityEngine;

public class RotateCameraByCompassExample2 : MonoBehaviour
{
    public int speed = 10;

    private OnlineMapsCameraOrbit cameraOrbit;
    private float targetAngle;
    private bool lerpTargetAngle;
    private Vector2 lastRotation;

    private void Start()
    {
        OnlineMapsLocationService.instance.OnCompassChanged += OnCompassChanged;

        cameraOrbit = OnlineMapsCameraOrbit.instance;
        lastRotation = cameraOrbit.rotation;
        targetAngle = lastRotation.y;
    }

    private void OnCompassChanged(float f)
    {
        targetAngle = f * 360;
        float angle = cameraOrbit.rotation.y;
        if (angle - targetAngle > 180) targetAngle -= 360;
        else if (targetAngle - angle > 180) targetAngle += 360;

        lerpTargetAngle = true;
    }

    private void Update()
    {
        if (lastRotation != cameraOrbit.rotation) lerpTargetAngle = false;

        if (lerpTargetAngle)
        {
            float newAngle = Mathf.Lerp(lastRotation.y, targetAngle, Time.deltaTime * speed);
            if (Mathf.Abs(newAngle - lastRotation.y) < 0.1)
            {
                lerpTargetAngle = false;
                cameraOrbit.rotation.y = targetAngle;
            }
            else cameraOrbit.rotation.y = newAngle;

            lastRotation = cameraOrbit.rotation;
        }
    }
}

2. Use OnlineMapsCameraOrbit.OnCameraControl to add an offset for the camera.
Something like:

using System;
using UnityEngine;

public class SetCameraOffset : MonoBehaviour
{
    public Vector3 centerOffset = new Vector3(0, 0, -300);
    private OnlineMapsCameraOrbit cameraOrbit;
    private OnlineMaps map;
    private OnlineMapsTileSetControl control;

    private void Start()
    {
        map = OnlineMaps.instance;
        control = OnlineMapsTileSetControl.instance;
        cameraOrbit = OnlineMapsCameraOrbit.instance;
        cameraOrbit.OnCameraControl += OnCameraControl;
    }

    private void OnCameraControl()
    {
        Vector2 rotation = OnlineMapsCameraOrbit.instance.rotation;
        float distance = OnlineMapsCameraOrbit.instance.distance;
        float rx = 90 - rotation.x;
        if (rx > 89.9) rx = 89.9f;

        double px = Math.Cos(rx * Mathf.Deg2Rad) * distance;
        double py = Math.Sin(rx * Mathf.Deg2Rad) * distance;
        double pz = Math.Cos(rotation.y * Mathf.Deg2Rad) * px;
        px = Math.Sin(rotation.y * Mathf.Deg2Rad) * px;

        Vector3 targetPosition = map.transform.position;
        Vector3 offset = new Vector3(control.sizeInScene.x / -2, 0, control.sizeInScene.y / 2);
        offset += Quaternion.Euler(0, rotation.y, 0) * centerOffset;

        if (OnlineMapsElevationManagerBase.useElevation)
        {
            double tlx, tly, brx, bry;
            map.GetCorners(out tlx, out tly, out brx, out bry);
            float yScale = OnlineMapsElevationManagerBase.GetBestElevationYScale(tlx, tly, brx, bry);
            if (cameraOrbit.adjustTo == OnlineMapsCameraAdjust.maxElevationInArea) offset.y = OnlineMapsElevationManagerBase.instance.GetMaxElevation(yScale);
            else offset.y = OnlineMapsElevationManagerBase.GetElevation(targetPosition.x, targetPosition.z, yScale, tlx, tly, brx, bry);
        }

        offset.Scale(map.transform.lossyScale);
        targetPosition += map.transform.rotation * offset;

        Vector3 newPosition = map.transform.rotation * new Vector3((float)px, (float)py, (float)pz) + targetPosition;

        control.activeCamera.transform.position = newPosition;
        control.activeCamera.transform.LookAt(targetPosition);
    }
}

3. Yes, you can try using the touch frameworks.

Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.

Re: Smooth Rotate with Camera Rotate to Compass

Thanks for the swift response.  Still learning the appropriate methods in customizing the existing scripts.  For the attached solution above am I modifying the existing examples or adding an additional script in addition to the ones I mentioned I am already using?

Re: Smooth Rotate with Camera Rotate to Compass

It is better to create new scripts in order not to lose your changes when updating asset.

Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.

6 (edited by tamim.ali.zoabi 2024-10-24 20:31:21)

Re: Smooth Rotate with Camera Rotate to Compass

Hi is that two choices scripts and i can use one of them
and if yes do i keep the :
locationService.OnCompassChanged += OnCompassChanged;

cause i used cameraOrbit but the rotation is kind of glitchy and not smooth . all the time the map trembling when i hold the phone and walk .
how to have a smooth Rotation without map vibration.
secondly i have a problem : the map rotate but once i use two finger that stretch map imidiatly the map stop rotate with compaschanged , can i disable two finger use .
Thanks

Re: Smooth Rotate with Camera Rotate to Compass

Hi.
1. An example of how to improve this is attached.
2. This is because when you use two fingers, Camera Orbit expects that you may want to rotate the map.
To disable this, use Camera Orbit / Lock Pan - ON, Lock Tilt - ON.

Post's attachments

Attachment icon RotateCameraByCompassExample2.cs 1.07 kb, 208 downloads since 2024-10-25 

Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.

Re: Smooth Rotate with Camera Rotate to Compass

I have another problem 
The orientation of the map is nut suitable some times when I drive the market set correctly and the map is OK when I make any round the map begin to be not  up and down

Re: Smooth Rotate with Camera Rotate to Compass

Unfortunately, I didn't understand your description of the problem.
Try to explain it in more detail.
And I think a small video in this case will help a lot to explain it.

Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.