Topic: Camara Orbit Tremling
hi
i used Orbit Camera when i move with my device the map tremling .
i will include a video and some screen shots about my code and live video about tremling map and olso a screen shot about map settings.
and i whant after correct the problem to have the RED arrow marker to stacked and stay look to front whithout rotating with the map when compas change .
Video Link:
https://www.loom.com/share/9bc595d817bc … 21e8210523
My Script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class MapManager : MonoBehaviour
{
public OnlineMaps map;
public GameObject startMarkerPrefab;
public GameObject destinationMarkerPrefab;
//public string googleAPIKey; // Your Google API Key
private string googleAPIKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
private OnlineMapsMarker startMarker;
private OnlineMapsMarker destinationMarker;
private Vector2 currentLocation;
private Vector2 destinationLocation;
private bool routePlotted;
public Text debug;
private float updateInterval = 1f; // How often to update the marker position
private float updateTimer; // Timer to manage update intervals
public GameObject StartHandlr, RoomHandlr, GPSHandlr;
//public OnlineMapsControlBase3D control;
public OnlineMapsCameraOrbit cameraOrbit;
public Transform TcameraOrbit;
public Texture2D MarkerS, MarkerD;
float latitude, longitude;
OnlineMapsLocationServiceBase locationService;
public int speed = 10;
private float targetAngle;
private bool lerpTargetAngle;
private Vector2 lastRotation;
private void Start()
{
if (string.IsNullOrEmpty(googleAPIKey))
{
Debug.LogWarning("Please specify your Google API Key.");
return;
}
if (map == null) map = OnlineMaps.instance;
if (cameraOrbit == null) cameraOrbit = OnlineMapsCameraOrbit.instance;
//locationService.OnLocationChanged += OnLocationChanged;
//locationService.OnCompassChanged += OnCompassChanged;
//debug.text = "Start Function";
locationService = OnlineMapsLocationService.instance;
StartCoroutine(StartLocationService());
// OnlineMapsLocationServiceBase.OnLocationInited
//locationService.OnLocationChanged += OnLocationChanged;
//locationService.OnCompassChanged += OnCompassChanged;
//locationService.desiredAccuracy = 10; // Meters. Lower means more accuracy.
//locationService.updateDistance = 5; // Meters. This sets how far you must move before an update is triggered.
//the rotation is kind of glitchy
}
private IEnumerator StartLocationService()
{
//debug.text = "Start Location Service.";
Input.location.Start();
while (Input.location.status == LocationServiceStatus.Initializing)
{
yield return new WaitForSeconds(1);
}
if (locationService != null)
{
// Subscribe to the OnLocationChanged and OnCompassChanged events
locationService.OnLocationChanged += OnLocationChanged;
locationService.OnCompassChanged += OnCompassChanged;
lastRotation = cameraOrbit.rotation;
targetAngle = lastRotation.y;
// Enable the location service if it's not already enabled
if (!locationService.isActiveAndEnabled)
{
locationService.SetStarted(true);
debug.text = "locationService Start.";
}
}
else
{
Debug.LogError("OnlineMapsLocationService instance is null. Make sure it's added to your scene.");
}
if (Input.location.status == LocationServiceStatus.Running)
{
//debug.text = "Location services are enabled and running";
// Get the current location
currentLocation = new Vector2(Input.location.lastData.longitude, Input.location.lastData.latitude);
//debug.text = "Location services are running.";
PlaceStartMarker(currentLocation);
}
else
{
Debug.LogError("Location services are disabled or not running.");
//debug.text = "Location services are disabled or not running.";
}
}
public void SearchAndPlotRoute(string address)
{
if (!string.IsNullOrEmpty(address))
{
OnlineMapsGoogleGeocoding geocoding = new OnlineMapsGoogleGeocoding(address, googleAPIKey);
geocoding.OnComplete += OnGeocodingComplete;
geocoding.Send();
}
else
{
Debug.LogError("Address input is empty.");
}
}
private void OnGeocodingComplete(string result)
{
Vector2 position = OnlineMapsGoogleGeocoding.GetCoordinatesFromResult(result);
if (position != Vector2.zero)
{
destinationLocation = position;
PlaceDestinationMarker(destinationLocation);
// Once destination is set, plot the route.
StartCoroutine(GetRoute(currentLocation, destinationLocation));
}
else
{
Debug.LogError("Failed to find the location.");
}
}
private void PlaceStartMarker(Vector2 location)
{
if (startMarker == null)
{
startMarker = OnlineMapsMarkerManager.CreateItem(location,MarkerS, "Start Marker");
//startMarker.texture = MarkerS;
startMarker.scale = 0.03f;
//startMarker.Init();
//debug.text="Place Start Markern.";
}
else
{
startMarker.position = location;
}
map.SetPositionAndZoom(location.x, location.y, 16);
}
private void PlaceDestinationMarker(Vector2 location)
{
if (destinationMarker == null)
{
destinationMarker = map.markerManager.Create(location,MarkerD, "Destination Marker");
//destinationMarker.texture = MarkerD;
destinationMarker.scale = 0.03f;
//destinationMarker.Init();
//debug.text = "Place Destinatio nMarker.";
}
else
{
destinationMarker.position = location;
}
}
private IEnumerator GetRoute(Vector2 start, Vector2 end)
{
OnlineMapsGoogleDirections request = new OnlineMapsGoogleDirections(googleAPIKey, start, end);
request.OnComplete += OnRouteComplete;
yield return null;
}
private void OnRouteComplete(string result)
{
//Debug.Log(response);
OnlineMapsOpenRouteServiceDirectionResult result1 = OnlineMapsOpenRouteServiceDirections.GetResults(result);
if (result == null || result1.routes.Length == 0)
{
Debug.Log("Open Route Service Directions failed.");
return;
}
// Get the points of the first route.
List<OnlineMapsVector2d> points = result1.routes[0].points;
// Draw the route.
OnlineMapsDrawingLine line = new OnlineMapsDrawingLine(points, Color.red);
map.drawingElementManager.Add(line);
// Set the map position to the first point of route.
map.position = points[0];
StartCoroutine(MoveMarkerAlongRoute(points));
}
private IEnumerator MoveMarkerAlongRoute(List<OnlineMapsVector2d> points)
{
foreach (Vector2 point in points)
{
startMarker.position = point;
yield return new WaitForSeconds(0.1f); // Simulate movement
}
}
public void ReturnToRoom()
{
GPSHandlr.SetActive(false);
float leftMargin = 0f; // Adjust the left margin
float rightMargin = 0f; // Adjust the right margin
RectTransform rectTransform = RoomHandlr.GetComponent<RectTransform>();
rectTransform.offsetMin = new Vector2(leftMargin, rectTransform.offsetMin.y);
rectTransform.offsetMax = new Vector2(-rightMargin, rectTransform.offsetMax.y);
rectTransform = StartHandlr.GetComponent<RectTransform>();
rectTransform.offsetMin = new Vector2(leftMargin, rectTransform.offsetMin.y);
rectTransform.offsetMax = new Vector2(-rightMargin, rectTransform.offsetMax.y);
}
private void OnDisable()
{
// Stop the location service when the script is disabled
Input.location.Stop();
}
private void OnCompassChanged(float f)
{
cameraOrbit.rotation.y = f * 360;
//targetAngle = f * 360;
//float angle = cameraOrbit.rotation.y;
//if (angle - targetAngle > 180) targetAngle -= 360;
//else if (targetAngle - angle > 180) targetAngle += 360;
//lerpTargetAngle = true;
}
//This event occurs at each change of GPS coordinates
private void OnLocationChanged(Vector2 position)
{
//Change the position of the marker to GPS coordinates
startMarker.position = position;
//debug.text = "On Location Changed.";
map.Redraw();
}
void Update()
{
// Disable two-finger gestures by checking touch count
if (Input.touchCount > 1)
{
// Prevent any two-finger interaction with the map
return;
}
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;
}
}
}