IEnumerator MoveNavi(Transform navi, Transform rig_f_middle, Vector3 a, Vector3 b, float speed)
{
float step = (speed / (a - b).magnitude) * Time.fixedDeltaTime;
float t = 0;
while (t <= 1.0f)
{
t += step; // Goes from 0 to 1, incrementing by step each time
navi.position = Vector3.Lerp(a, b, t); // Move objectToMove closer to b
var distance = Vector3.Distance(a, b);
if(distance < 0.05f)
{
var emptyobject = new GameObject();
emptyobject.transform.parent = rig_f_middle.transform;
emptyobject.transform.localPosition = new Vector3(0, 0, 0);
emptyobject.transform.localRotation = rig_f_middle.parent.localRotation;
navi.parent = emptyobject.transform;
navi.GetComponent<InteractableItem>().distance = 0;
break;
}
yield return new WaitForFixedUpdate(); // Leave the routine and return here in the next frame
}
navi.position = b;
}
Before that I tried it without using an empty new GameObject just tried to set NAVI it self to be child of ig_f_middle and then tried to set NAVI to set NAVI position to 0,0,0 and rotation also to 0,0,0 tried also with local position and local rotation but nothing was working so I tried with creating first a new empty GameObject make him child of ig_f_middle and then change the new empty GameObject position and rotation to be 0,0,0 and then make NAVI child of the new empty GameObject but same results the new empty GameObject transform is never 0,0,0 and also NAVI is never 0,0,0
First NAVI was child of another object and now I want it to move smooth slowly and become a child of another object the rig_f_middle.02.R object.
If the game is running and after NAVI become child of rig_f_middle.02.R and then in the Inspector I will change the position and rotation to 0,0,0 it will work fine as I wanted but through the script I never success to make NAVI transform to be set to 0,0,0
Maybe the whole problem is because Player object have Animator component ? That’s why I’m using a new empty GameObject but it’s not working.