- Home /
Question by
Witherik · Aug 20, 2017 at 10:32 AM ·
movementtransform.position
Move from one position to other in one second
I tried using
Vector3.MoveTowards
But it moves the object based on speed speed not time... Is there a certain command, or should I calculate the range between two positions and calculate the speed?
Comment
Best Answer
Answer by Cherno · Aug 20, 2017 at 11:00 AM
void Start() {
StartCoroutine(MoveToPos(new Vector3(0,3,0));
}
public void IEnumerator MoveToPos(Vector3 endPos) {
Vector3 startPos = transform.position;
float t = 0f;
while(t < 1f) {
transform.position = Vector3.Lerp(startPos, endPos, t);
t += Time.deltaTime;
yield return null;
}
}
Answer by DrZarqawi · Aug 20, 2017 at 10:59 AM
I'm not really sure how you want this?
But what i think you want is something along the lines of:
Vector3 target;
float TimeInSeconds, distance;
void Start () {
distance = Vector3.Distance(transform.position, target);
}
void Update () {
transform.position = Vector3.MoveTowards(transform.position, target, (distance/TimeInSeconds)*Time.deltaTime);
}
Your answer
Follow this Question
Related Questions
Character Transform shifting after colliding with objects 0 Answers
Vector3 position changing without force being applied in the direction 1 Answer
why ragged ( not smooth) movement....hlp plz 1 Answer
'jerking' movement... 1 Answer
Oculus Quest player controller not moving on the vertical axis when using New Vector3 script 0 Answers