The problem is that I'm using Lerp and if I'm making the transform to move from the originPosition to the target(destinationTransform) it's not moving from the player hand but from another position near the player.
This screenshot is the transform object in the player hand as the original position it's should be the original position :
Then when the transform start moving to the target he start moving not from the hand but from a position near the hand :
This line is making the transform moving :
transform.position = Vector3.Lerp(originPosition, destinationTransform.position, curve.Evaluate(s));
If I'm changing in this line from originPosition to transform.position then it will move from the hand but the duration will not be 10 seconds or the time I'm setting.
transform.position = Vector3.Lerp(transform.position, destinationTransform.position, curve.Evaluate(s));
What I'm trying to do is to move the transform to the target stay at the target hang there X seconds and the start moving back.
and only when it's moving back top make the transform in the end to be Transferring as child of the target(the player hand) when reaching to the target that is not the player hand don't make it child only on the way back.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveToTarget : MonoBehaviour
{
public enum TransitionState
{
None,
MovingTowards,
Transferring
}
public Transform destinationTransform;
public bool isChild = false;
public AnimationCurve curve = AnimationCurve.EaseInOut(0.0f, 0.0f, 1.0f, 1.0f);
public float duration = 10.0f;
public bool go = false;
private float t;
private Transform originTransform;
private float timer;
private TransitionState state = TransitionState.MovingTowards;
private Vector3 originPosition;
private SphereCollider col;
private bool enableCollider = true;
void Start()
{
t = 0.0f;
curve.postWrapMode = WrapMode.Once;
originPosition = transform.position;
col = GetComponent<SphereCollider>();
}
void Update()
{
if (go)
{
if (col != null && enableCollider)
{
//col.enabled = false;
//transform.GetComponent<Rigidbody>().isKinematic = true;
Destroy(col);
Destroy(transform.GetComponent<Rigidbody>());
transform.GetComponent<InteractableItem>().enabledInteraction = false;
enableCollider = false;
}
switch (state)
{
case TransitionState.MovingTowards:
var v = destinationTransform.position - transform.position;
if (v.magnitude < 0.001f)
{
state = TransitionState.Transferring;
originTransform = destinationTransform;
timer = 0;
return;
}
t += Time.deltaTime;
float s = t / duration;
transform.position = Vector3.Lerp(originPosition, destinationTransform.position, curve.Evaluate(s));
break;
case TransitionState.Transferring:
timer += Time.deltaTime;
this.transform.position = Vector3.Lerp(originTransform.position, destinationTransform.position, timer);
if (timer >= 1.0f)
{
this.transform.parent = destinationTransform;
transform.localPosition = new Vector3(0, 0, 0);
isChild = true;
go = false;
state = TransitionState.None;
this.enabled = false;
return;
}
break;
default:
this.enabled = false;
return;
}
}
}
}