- Home /
C# trying to move a object
Hello, Im trying to move an object to a certain posistion as I have here
objectToMove.transform.position = new Vector3(-75, 6, -13);
And I encounterd a problem, the object moves instantly to that position as I want it to move slowly to that position (Like a space door thats sliding to the side)
I'm using a GUI button to move the door incase that matters.
Thanks in advance.
Answer by giano574 · Apr 03, 2014 at 09:46 AM
If you have a specific point that you want your player to move to, Vector3.MoveTowards is ideal. Use it like this:
float speed = 10 * Time.deltaTime;
objectToMove.transform.position = Vector3.MoveTowards(objectToMove.transform.position, targetPosition, speed);
Note that Vector3.MoveTowards returns a Vector3 that is 'speed' closer to the targetPosition. This means that you can check its value and only change the position of the Transform if it is not equal to. For further reference, see the Unity docs: https://docs.unity3d.com/Documentation/ScriptReference/Vector3.MoveTowards.html
Answer by ChrisJoosten · Apr 03, 2014 at 08:20 AM
Not with the code you are using.
You are saying $$anonymous$$ove Player to this position, Time.deltaTime doesn't have any use in that..