The problem I can't solve is: cloned object, slow progression of parent object to X axis. But no matter what I did I could do it based on time, this is the latest version of the code.
public GameObject Ball;
private void OnTriggerExit(Collider other)
{
if (other.CompareTag("Gate"))
{
GameObject clone2 = Instantiate(Ball, transform.position, transform.rotation);
transform.position = Vector3.Lerp(transform.position, new Vector3(transform.position.x * 2,
transform.position.y,
transform.position.z), 25);
clone2.transform.parent = gameObject.transform;
}
if (other.CompareTag("Push"))
{
Destroy(gameObject);
}
}
CodePudding user response:
@BugFinder already mentioned it in the comments. But the likely issue is that OnTriggerExit only gets called once and moves only one frame. Not only that but you are moving it over 25 seconds. Which is a long time for a Lerp function.
Below is a solution you can try:
float speed = 5;
private void OnTriggerExit(Collider other)
{
if (other.CompareTag("Gate"))
{
GameObject clone2 = Instantiate(Ball, transform.position, transform.rotation);
Vector3 targetLocation = new Vector3(transform.position.x * 2, transform.position.y, transform.position.z);
StartCoroutine(MoveTowardsLocation(clone2.transform, targetLocation, speed));
}
else if (other.CompareTag("Push"))
{
Destroy(gameObject);
}
}
private IEnumerator MoveTowardsLocation(Transform movedObj, Vector3 targetLocation, float speed)
{
Vector3 direction = (movedObj - movedObj.position).normalized;
while(Vector3.Distance(movedObj.position, targetLocation) > 0.01f)
{
movedObj.position = Vector3.Lerp(movedObj.position, movedObj.position direction * speed * Time.deltatime, 1f);
yield return null;
}
movedObj.position = targetLocation;
}
CodePudding user response:
In your code Lerp is called only once on trigger exit and position is updated only in one frame which is not resulting to visible change. To fix it you need start a coroutine on trigger exit and change transform.position in coroutine.