Reputation: 11341
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;
}
// Leave the routine and return here in the next frame
yield return new WaitForFixedUpdate();
}
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.
Upvotes: 0
Views: 305
Reputation: 90872
Your don't want to copy the parents localRotation
but you rather want to set the localRotation
to zero
navi.parent = rig_f_middle;
navi.localRotation = Quaternion.identity;
navi.localPosition = Vector3.zero;
This should actually do what you describe. There is no need for the additional empty object .. it makes it even more complex.
Alternatively you could also copy the actual absolute world position and orientation of the parent
navi.parent = rig_f_middle;
navi.position = rig_f_middle.position;
navi.rotation = rig_f_middle.rotation;
Note that in the end of the routine you also set the position of
navi.position = b;
which might of course undo the parenting and setting to zero before.
Note: If navi
is indeed the object with an Animator
attached then yes, that is a problem!
The Animator
routine is one of the last things executed in a frame and as soon as it has only one single key frame on a property like e.g. the position it will always overrule any changes made by code!
The only way around would then probably be disable the component during your Coroutines.
And btw. you should rather recalculate the step
every frame instead of storing it only once since next time the Time.deltaTime
will be a different value.
Probably something like
IEnumerator MoveNavi(
Transform navi,
Transform rig_f_middle,
Vector3 a,
Vector3 b,
float speed)
{
var animator = navi.GetComponent<Animator>();
animator.enabled = false;
var t = 0f;
while (t < 1.0f)
{
var step = (speed / (a - b).magnitude) * Time.deltaTime;
t += step;
navi.position = Vector3.Lerp(a, b, t);
var distance = Vector3.Distance(a, b);
if(distance < 0.05f)
{
break;
}
// Leave the routine and return here in the next frame
yield return new WaitForFixedUpdate();
}
navi.GetComponent<InteractableItem>().distance = 0;
navi.parent = rig_f_middle;
navi.localRotation = Quaternion.identity;
// Here you clearly set a different position ...
navi.position = b;
// As soon as you enable the Animator component again
// The position might again change if there is any key frame for it ...
animator.enabled = true;
}
Upvotes: 2