Lerp to Gaze Location Smoothing Motion Issues (Cardboard)
Hey guys.
Im trying to create a script that allows my camera (player) to move between waypoints within a VR scene.
I have a script for a basic 'stare button' and would like the player to move to the location of the button once it has been looked at. Thus far the script it working to the extent that it 'teleports' the player to the new location, could anyone help me out with smoothing this so it happens over a few seconds?
using System;
using System.Reflection;
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.Events;
using UnityEngine.UI;
public class StareButton2 : MonoBehaviour, IEventSystemHandler, IPointerExitHandler, IPointerEnterHandler, ISelectHandler
{
Transform startMarker;
public Transform endMarker;
public float speed = 1.0F;
private float startTime;
private float journeyLength;
// Stare Variables
[SerializeField]
private ButtonClickedEvent _OnClick = new ButtonClickedEvent();
public float fillSpeed = 2f;
Text _buttonLabel;
public Image radialFill;
bool _selected;
bool _staring;
void Awake()
{
_buttonLabel = this.gameObject.GetComponentInChildren<Text>();
startTime = Time.time;
journeyLength = Vector3.Distance(startMarker.position, endMarker.position);
// startMarker = Camera.main.transform;
}
void Start()
{
Reset();
}
void Update()
{
if (_staring)
radialFill.fillAmount += (Time.deltaTime * fillSpeed);
else
radialFill.fillAmount -= (Time.deltaTime * fillSpeed);
if (radialFill.fillAmount >= 1f)
{
radialFill.fillAmount = 1f;
if (!_selected)
{ //prevent multiple triggers
_selected = true;
_OnClick.Invoke();
Debug.Log("CLICK!");
}
}
else if (radialFill.fillAmount <= 0f)
{
_selected = false;
radialFill.fillAmount = 0f;
}
}
public void StaringAt(bool on)
{
_staring = on;
}
public void Reset()
{
_staring = false;
_selected = false;
radialFill.fillAmount = 0;
}
public void OnPointerEnter(PointerEventData eventData)
{
StaringAt(true);
}
public void OnPointerExit(PointerEventData eventData)
{
StaringAt(false);
}
public void OnSelect(BaseEventData eventData)
{
_OnClick.Invoke();
radialFill.fillAmount = 1f;
_selected = true;
Debug.Log("SELECT?!?!?!!");
float distCovered = (Time.time - startTime) * speed;
float fracJourney = distCovered / journeyLength;
transform.position = Vector3.Lerp(startMarker.position, endMarker.position, fracJourney);
}
[Serializable]
public class ButtonClickedEvent : UnityEvent { }
}
Comment
Your answer
Follow this Question
Related Questions
Why isn't my object lerping ? C# 2 Answers
Smooth movement for a 3rd person camera 2 Answers
Determine a radius on terrain and lerp the border to smooth the height flattening 0 Answers
Rotate on Transform.localeulerAngels smoothly 0 Answers
How to smoothly rotate gameobject 20 degrees on key press? 0 Answers