- Home /
Object keeps orbiting target location in a Coroutine
this is my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.AI;
public class debugMovement : MonoBehaviour
{
public float speed = 5;
Coroutine DASH = null;
public void OnCancel()
{
StopCoroutine(DASH);
}
public void OnClick(InputValue value)
{
Ray mouseRay = Camera.main.ScreenPointToRay(Mouse.current.position.ReadValue());
if(Physics.Raycast(mouseRay, out RaycastHit hit))
{
if (DASH != null)
StopCoroutine(DASH);
DASH = StartCoroutine(dash(hit.point, speed));
}
}
IEnumerator dash(Vector3 target, float speed)
{
while(Vector3.Distance(transform.position, target) >= .05f)
{
transform.position = Vector3.Lerp(transform.position, target, speed * Time.deltaTime);
yield return null;
}
yield return null;
print("dash ended");
}
}
The coroutine never finishes and the object keeps orbiting and jittering around the target location.
Is there a way to avoid this?
Something like AutoBraking in the NavmeshAgent.
And preferably using a coroutine, as I'm using it in a method so it can be triggered in the "Player input Component". However, if there is a better way to do it please let me know.
Thank you
Comment
Your answer
Follow this Question
Related Questions
Moving multiple transforms from an array in a single script 0 Answers
How to stop coroutines or functions 2 Answers
Translating Object Continually 1 Answer
Look Where You're Going 3 Answers
How to make a object go and come back with a mouse click 1 Answer