I think because it’s repeatedly being called every 0.3F it doesn’t have time to complete the lerp.
Any help would be greatly appreciated thanks!
float clientUpdateRate = 0.3F;
float lastMovementUpdate;
float posistionSmothing = 2;
void Update(){
if (Time.time - lastMovementUpdate > clientUpdateRate) {
lastMovementUpdate = Time.time;
//I know I don't have cnnId, or the position floats declared.
OnMayPosition(cnnId, x, y, z);
}
}
void OnMyPosition(int cnnId, float x, float y, float z) {
Vector3 oldPosition = clients.Find(p => p.connectionId == cnnId).position;
Vector3 newPosition = new Vector3(x, y, z);
clients.Find(p => p.connectionId == cnnId).position = Vector3.Lerp(oldPosition, newPosition, posistionSmothing);
}
I think you expect lerp to do all your work, but everytime you call lerp, its just one step, so make sure to call lerp every one so often with the correct increasing values. And if you don’t want another lerp happening just use a bool.
//global variable
bool lerping;
//coroutines variable
float startTime;
//lerped value (read-only if not coroutine)
float value;
//setup here
void Start() {
lerping = false;
}
//call this if you want to lerp
void someFunction () {
if(lerping == false) {
lerping = true;
startTime = Time.time;
StartCoroutine(letsLerp(0.0f, 1.0f));
}
}
//lerps from a to b based on time and writes to value
IEnumerator letsLerp(float a, float b) {
float t = Time.time - startTime;
value = lerp(a, b, t);
if (value < b)
yield return null;
lerping = false;
}
Note that in your comment you used “yield return” directly. You should use “yield return” to return in this frame but in the next to come back, a return otherwise finishes a function. The yield means to finish the work in later cycle.
In my coroutine above I yield back as long as the lerp is unfinished (value has not increased to b)
I havn’t tested this and expect the lerp to take some time, it is necessary to use some multiplier for the timer.