I was trying to create a code in unity.My aim was to move an object from current position on x-axis(y=0,z=0) to another point on x-axis(y=0 ,z=0).
The object was at (0,0,0) and i was trying to move it at (5,0,0).
With speed=2 the object should reach there in 2.5 seconds(i was using time.deltatime in code).
But it always take 2.48s if i use Vector.Lerp in code(1st code below).When i use movetoward in my another code exactly same happen(2nd code below).When i use distance remaining as a condition in if statement exactly same happen.(4th code below).Always 2.48 seconds instead of 2.50 seconds.
When i use journeytime as a condition in if statement in my third code, the object reach slightly far or near than 5units , for ex - 5.0125 etc.(3rd code below).
1.My lerp code
using UnityEngine;
public class movement1DLerp : MonoBehaviour {
private Vector3 startPosition;
public Transform transformTarget;
public float speed,journeyTime, journeyPercentage;
void Start () {
startPosition = transform.position;
journeyTime = Vector3.Distance(transformTarget.position,startPosition)/speed;
}
void Update () {
journeyPercentage += Time.deltaTime/journeyTime;
transform.position = Vector3.Lerp(startPosition,transformTarget.position,journeyPercentage);
if (transform.position== transformTarget.position)
{
Debug.Log(Time.fixedTime);
}
}
}
2.Movetoward code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movement1DMoveAround : MonoBehaviour {
public Transform TargetTransform;
public float speed,step;
//debuginfos
public float starttime,timelength,estimatedEndtime;
void Start() {
starttime = Time.time;
timelength = (TargetTransform.position.x-transform.position.x)/speed;
estimatedEndtime = starttime + timelength;
}
void Update () {
step = speed * Time.deltaTime;
transform.position=Vector3.MoveTowards(transform.position, TargetTransform.position, step);
if (transform.position == TargetTransform.position)
{
Debug.Log("Actual end time = " + Time.fixedTime);
}
}
}
3.MY journeytime as a condition in if statement CODE
using UnityEngine;
public class movement1DByTime : MonoBehaviour {
public Transform targetTransform; //transform of object placed at target position
public float speed,velocity,displacement,startTime,journeyTimeLength,stopTime,step;
private Vector3 NewPos;
void Start () {
displacement = targetTransform.position.x- transform.position.x ; //distance to be travelled
if (displacement < 0)
velocity = -speed;
else
velocity = speed;
startTime = Time.time; //time at beginning of execution of this code
journeyTimeLength = displacement / velocity; //total time to be taken by journey
stopTime = startTime + journeyTimeLength; //time at end of journey
NewPos = targetTransform.position;
}
// Update is called once per frame
void Update () {
if (Time.time<=stopTime)
{
NewPos.x = transform.position.x + velocity * Time.deltaTime;
transform.position = NewPos;
}
}
}
4.distance as a condition in if statement
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class _1DimMultiDirGo2PointOri : MonoBehaviour {
public int targetPos; //where object has to travel to
public float speed,step;
private float distance;
private Vector2 newPos;
void Update()
{
distance = targetPos - transform.position.x; //distance between current position and target position
step = speed * Time.deltaTime;
if (distance >= -step && distance <= step)
{ //A trap to stop potential oscillation if step will be beyond target
newPos.x = transform.position.x + distance;
transform.position = newPos;
}
else if (distance > 0)
{ //if target is ahead of current position
newPos.x = transform.position.x + step;
transform.position = newPos;
}
else if (distance < 0)
{ //if target is behind current position
newPos.x = transform.position.x - step;
transform.position = newPos;
}
if (transform.position.x == targetPos)
Debug.Log(Time.fixedTime);
}
}