Trying to Vector3.Lerp in coroutine
Hi, I try to move my player to the target position while player has clicked over the target boxcollider which i s set to isTrigger. My problem in my code is that when I click on it the player moves almost instantly (like teleporting) to the target position instead of smoothly pulling to it. I found out that doing Vector3.Lerp in IEnumerator is better for the smoothness but I didn;t managed to do it by myself. Could someone "transform" it to the Coroutine? (Script is attached to the target object)
private void OnMouseDown()
{
player.GetComponent<Rigidbody2D>().transform.position = Vector3.Lerp(player.transform.position, target.transform.position, Time.deltaTime / .25f);
}
Answer by xxmariofer · Jul 30, 2020 at 10:54 AM
using lerp in the ienumerator is not better for smoothnees, your player teleports because Time.deltaTime/.25 is too high reduce that value also remove GetComponetn()
Okay true i lowered the .25f but it gives me like "easing" effect when clicked on the target. How could i remove it and make it clean. And why should i remove GetComponent? If i remove it i won't be able to get the rb from player.
Change Lerp to $$anonymous$$ovetowards and adjust the speed again. You are noot accessig the rigidbody only the transform so its completly irrelevant, simple delete It but leave the transform...