# Is there any function to move object by localPosition?

Hello,

I build a small project to describe my question.

Here is my code.

``````    public GameObject player;
public GameObject weapon;

void Start()
{

player.transform.RotateAround(player.transform.position, new Vector3(0, 1, 0), 45);

//#1
//weapon.transform.localPosition += new Vector3(2, 0, 0);

//#2
//weapon.transform.position = Vector3.Slerp(weapon.transform.position, player.transform.position + new Vector3(2, 0, 0), 999);

}
``````

And when I run it, the â€śplayerâ€ť is rotated 45Â°

When I uncommented #1, the â€śweaponâ€ť got to the right position.

A smooth movement may be better, but when I commented #1 and uncommented #2, I got this.

I think I know what happened hereâ€¦ and I wonder is there any function to move object by localPosition?
Any help will be appreciated:)

In #2, why are you using the global position values? Try using the local values instead, like in #1.

Oopsâ€¦such a simple mistakeâ€¦

The problem your running into, is the weapon is a child of the player(cube). so you ARE moving it by â€ślocalPositionâ€ť. That is, its transform position is relative to the Cube. So when you were adding + Vector3(2,0,0) you just adjusting it position relative to the weapon.
But your Slerp was between (weaponâ€™s Local Position Relative to the Cube, Cubeâ€™s absolute Position in the world+2, time Factor). So the only reason the cylinder was even close to the cube was your cube was probably near the origin. If your cube had been at world position +100,0,+100. Then when you set the cylinderâ€™s local position to 102,0,100 it would have been that many units AWAY from the cube. and probably off your screen.

Your Slerp should work but change it to this: `weapon.transform.position = Vector3.Slerp(weapon.transform.position, weapon.transform.position + new Vector3(2, 0, 0), 999);`

Note: the timeFactor (999) is clamped in the Slerp code to the range 0->1. So your 999 is the same as typing 1.0 which will always return the full second position (weapon.transform.position)+new Vector3(2, 0, 0),.
If you want to move the cylinder slowly away from the cube try this:

``````    void Start () {
StartCoroutine(MoveCylinder());
}

IEnumerator MoveCylinder()
{
float currentTime = 0.0f;
float normalizedDelta;
Vector3 originalPosition = weapon.transform.position;
Vector3 destinationPosition = weapon.transform.position + new Vector3(5.0f, 0.0f, 0.0f);
while (currentTime < 3.0f)
{
currentTime += Time.deltaTime;
// since we are taking 3s to move
// We need to normalize that time in the range of 0->1
normalizedDelta = currentTime / 3.0f;
weapon.transform.position = Vector3.Lerp(originalPosition, destinationPosition, normalizedDelta);
yield return null;
}
}
``````

Also note I used LERP instead of SLERP. If your just moving in a straight line, Iâ€™d use LERP on the position vector as its Linear interpretion of it. SLERP takes both magnitude and the angle into account, so it will end up in the same place as LERP, but it can sometimes do a arc type of movement.

2 Likes

OMG the is the exact function Iâ€™ve been looking for, forever! I wanted my player to be able to walk up to a box an Climb it by hitting F. This is LIT!