- Home /
How to Slerp through values inside a 2D array ?
I have an 2D array which stores the transforms of a gameobject at every frame. My requirement is that when the gameobject reaches a checkpoint, the gameobject should go back to the first value and go through the same path again, but it doesn't have to be at a linear speed. Rather, it should start to speed up towards the end. I'm able to successfully make the gameobject go through the exact motion but unable to make it speed up towards the end. I basically wish to change the interpolation of how the array be able to speed up through its' values. The below is the snippet of how I'm making the gameobject go through the values stored inside the array at a linear speed.
myTrans tp = new myTrans (); //create temp variable our class
tp.myPos = this.transform.position; //save current position
tp.myQuat = this.transform.rotation; //save current rotation
tp.myScale = this.transform.localScale; //save current scale
Movements.Add (tp); //add current data in our list
MovementIndex++;
//This part above saves the values inside the array
MovementIndex++;
if (MovementIndex >= 0 && Movements.Count > 0) {
this.transform.position = Movements[MovementIndex].myPos;
this.transform.rotation = Movements[MovementIndex].myQuat;
this.transform.localScale = Movements[MovementIndex].myScale;
}
//This part above goes through the values inside the array making the object to go through the same path as before.
Answer by zombience · Sep 05, 2014 at 05:11 AM
one way you could do it is to use an AnimationCurve, draw in the interpolation curve that you'd want in the window, and then evaluate the linear value and return an interpolated value.
something like this:
public AnimationCurve curve; // assign this in the inspector window
public float linearInput = 0;
public float interpolatedOutput;
void Update()
{
linearInput += .01f;
interpolatedOutput = curve.Evaluate(linearInput);
}
as you run this script, you'll see interpolatedOutput move according to the curve that you've drawn in the AnimationCurve window in the inspector. I find this a quick and easy way to do such operations, because they are flexible and quick to edit. you can then multiply the output of your curve to match your desired range of output.
hope that helps
Hello @zombience, apologies for a late reply. I tried this workflow. This idea seems like a particularly interesting way to interpolate values. However I could not manage to get this to work with the results I was expecting. I was trying to reverse the motion and go through the values in the $$anonymous$$ovementIndex in reverse. I assigned a graph curve such that it speeds up towards the end, but the interpolated output seemed pretty much the same.
interpolatedOutput = curve.Evaluate($$anonymous$$ovementIndex--);
$$anonymous$$ovementIndex = $$anonymous$$athf.CeilToInt( interpolatedOutput);
if ($$anonymous$$ovementIndex >= 0 && $$anonymous$$ovements.Count > 0) {
this.transform.position = $$anonymous$$ovements[$$anonymous$$ovementIndex].myPos;
this.transform.rotation = $$anonymous$$ovements[$$anonymous$$ovementIndex].myQuat;
this.transform.localScale = $$anonymous$$ovements[$$anonymous$$ovementIndex].myScale;
}
sorry for the late reply
i just tested this script out, and the interpolation works. perhaps your curve is not steep enough to perceive a difference?
using UnityEngine;
using System.Collections;
public class ArrayTest : $$anonymous$$onoBehaviour
{
public AnimationCurve curve;
public float distance = 20;
public int points = 100;
public int idx;
[Range(0f, .2f)]
public float speed = .01f;
public float evaluation;
protected float index = 0;
protected Vector3 endPoint;
protected Vector3[] path;
void Start ()
{
endPoint = transform.position + transform.up * distance;
path = new Vector3[points];
Vector3 inc = (endPoint - transform.position).normalized * (distance / points);
for(int i = 0; i < points; i++)
{
if(i == 0)
path[i] = transform.position + inc;
else
path[i] = path[i-1] + inc;
}
}
void Update ()
{
index += speed;
if(index >= 1f || index < $$anonymous$$athf.Abs(speed))
speed *= -1f;
evaluation = curve.Evaluate(index) * (points - 1);
idx = $$anonymous$$athf.FloorToInt(curve.Evaluate(index) * (points - 1));
if(idx < 0)
idx = 0;
transform.position = path[idx];
}
}
Ah, that was a rookie mistake. It does work and my curve was not indeed not steep enough. Thanks a lot for taking the time to do this, @zombience. I very much appreciate this. :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613162334im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
curved path, Slerp issue? 1 Answer
Enemy offset, gallaga style game (space invaders) 0 Answers
Array doesn't increase or add element 1 Answer
Gameboard, next position on a path 2 Answers
Remove particular objects from array 0 Answers