# Object is moving in a different speed if the target is very far or very close how to const speed ?

I have this two methods and they are working and I want to keep using them but the problem is that if the target the objectToMove is should reaching too far or very far the speed of the movement of the objectToMove will be fast but if the target to reach is very close or closer the objectToMove will move very very slow.

I want to have a constant speed that I can set the speed no matter what distance is the objectToMove from the target.

``````IEnumerator ThrowObject(Transform objectToMove, float duration, InteractableItem primaryTarget)
{
float counter = 0;

while (counter < duration)
{
counter += Time.deltaTime;
Vector3 currentPos = objectToMove.position;

float time = Vector3.Distance(currentPos, primaryTarget.transform.position) / (duration - counter) * Time.deltaTime;

objectToMove.position = Vector3.MoveTowards(currentPos, primaryTarget.transform.position, time);

yield return null;
}

primaryTarget.gameObject.SetActive(false);

var naviParent = GameObject.Find("Navi Parent");
StartCoroutine(ThrowBack(objectToMove, naviParent.transform.position , duration, primaryTarget));
}
``````

And

``````    IEnumerator ThrowBack(Transform objectToMove,Vector3 originalPosition , float duration, InteractableItem primaryTarget)
{
float counter = 0;

while (counter < duration)
{
counter += Time.deltaTime;
Vector3 currentPos = objectToMove.position;

float time = Vector3.Distance(currentPos, originalPosition) / (duration - counter) * Time.deltaTime;
objectToMove.position = Vector3.MoveTowards(currentPos, originalPosition, time);
//yield return null essentially means "return here every frame"
yield return null;
}

objectToMove.localPosition = new Vector3(0, 0, 0);

if (primaryTarget.name == "kid_from_space_helmet")
{
var helmetParent = GameObject.Find("Helmet Parent");
GameObject Helmet = helmetParent.transform.Find("kid_from_space_helmet").gameObject;
Helmet.SetActive(true);
}

primaryTarget.interactableMode = InteractableItem.InteractableMode.Description;
primaryTarget.distance = 0;
}
``````

And how Iâ€™m using it :

``````StartCoroutine(ThrowObject(objToThrow.transform, throwSpeed, primaryTarget));
``````

Because itâ€™s working I want to keep it this way with the two methods just to change it that the speed will be the same no matter what is the distance between the objectToMove and the primaryTarget.

Youâ€™re doing some complicated calculation based on the distance to figure out how far to allow your object to move here:

``````            float time = Vector3.Distance(currentPos, primaryTarget.transform.position) / (duration - counter) * Time.deltaTime;
``````

but if you want a constant speed, all you need to do is this:

``````float speed = 5f; // 5 units per second
objectToMove.position = Vector3.MoveTowards(currentPos, primaryTarget.transform.position, speed * Time.deltaTime);
``````
1 Like

Few things :

Not as before after the changes in both methods when the objectToThrow is coming back to the player hand now when the player is moving again when Iâ€™m moving the player the objectToThrow is not coming with the player hand itâ€™s left behind and moving randomly jump strange. The objectToMove is a child and Iâ€™m moving it to a target(primaryTarget) then when itâ€™s reaching the target the target is set to non active disable then the object should move back to the player hand.

``````primaryTarget.gameObject.SetActive(false);
``````

I simulate this way a picking up object.

You can see it in this screenshot :
The NAVI object is child of Navi Parent. Both Navi Parent and NAVI positions are set to 0,0,0
NAVI is the object that move to the primaryTarget in this case itâ€™s the Cube in the screenshot.
And NAVI is also moving back to the player hand back and should stay child of Navi Parent just move to the target and back.

The player hand object that holding NAVI is rig_f_middle.02.R (Navi Parent is child of rig_f_middle.02.R and NAVI is child of Navi Parent).

The problem is that the NAVI is child so the problem is to make it move to the target and back and keep staying child.
That is why in the second method Iâ€™m also doing :

``````objectToMove.localPosition = new Vector3(0, 0, 0);
``````

To make sure the NAVI sill be child of the Navi Parent when moving the player again.
Itâ€™s not clean solution but itâ€™s working. I could not find another way with sending the NAVI to the target and back itâ€™s never returning to the hand at 0,0,0 and if Iâ€™m not setting it on my own to 0,0,0 then when moving the player the NAVI will not move with the hand fine.

After changing your lines solution in both methods ThrowObject and ThrowBack for some reason the NAVI is not returning to the hand fine and not moving with the players hand.

Before it did work fine but the sped was not fine.

I think you have labelled the variable â€śtimeâ€ť wrong. Itâ€™s a step, not time.

In that case you have to save the values of currentPos and originalPos so that it does not change on each loop.
Right now the currentPos is changing. Because it is being used inside the loop.

`````` while (counter < duration  )
{
....
Vector3 currentPos = objectToMove.position;
....
float step = Vector3.Distance(currentPos, originalPosition) / (duration - counter) * Time.deltaTime
}
``````

Try moving it out of the loop.

``````Vector3 currentPos = objectToMove.position;
while (counter < duration  )
{
....
....
float step = Vector3.Distance(currentPos, originalPosition) / (duration - counter) * Time.deltaTime
}
``````