I don’t know if it is too late already or if I am too stupid. Somehow, I wanted to write my own interpolation code for moving objects. The idea is to have a starting point, an end point and a waypoint. Now this waypoint shall be interpolated so that start = start, end = end and half way through the waypoint is either tangented for a split second or not (I wanna make this configurable). So imagine a -y = x² curve where (0/0) is my waypoint to be tangented or “missed” by like 5% of the way between start (somewhere on (-1/-1)) and wp and (1/-1) being end.
My idea is to Lerp to a vector which is t% vector wp and 1-t% vector end where t=percentual time it takes to move. My method so far is this:
But the result is not what I want (way too flat). Also tried all sorts of Pow(), t-1, 1-t, etc. But I think my main idea of composing a vector like this may be wrong. Maybe I missed sth and I can even use existing functions, but I couldn’t understand how to use SmoothDamp() so I decided to “quickly” write my own function. Good thing I only wasted roughly 2h on that …
As an example I might have a magic spell that moves in a specific way. Imagine a wizard summoning a growing power ball that moves from (0/2/0) to (0/1/0) but shortly before reaching (0/1/0) moves quickly to the location of the opponent, e.g. (5/1/1).
@Kiwasi what does this tell me? Are there existing functions for splines?
@ShilohGames can you post an example of how this would work?
If you want to move something along a curved path, you are after bezier curves as mentioned. If you want to specify the points the curve should pass though and have something calculate the curve automatically, you are after splines.
If you want to specify how thing move along your path or curve (e.g. slow at first, or slow at the end, etc) you are probably interested in easing functions.
My waypoints are just very few, you maybe I am not talking a bout a curved path,
splines sound like the ones I am searching for,
I can achieve the easing myself, as my script already includes a time it takes to go the way.
So it seems I need to delve more into splines. Is there something ready-to-use already?
I would probably do this with two steps: use animation curve or perhaps Animation itself for fancy movement after summoning, and then just move towards target.
try this test script to get idea how to use animation curve for this kind of movement:
using UnityEngine;
using System.Collections;
public class AniCurveTest : MonoBehaviour {
public AnimationCurve aniCurve;
[Range(0f,1f)]
public float normalizedTime;
public float scale =1f;
public float curveValue;
float curveLenghtInSeconds;
Vector3 tempVector =new Vector3();
void Update () {
Keyframe lastframe= aniCurve[aniCurve.length -1];
curveLenghtInSeconds = lastframe.time;
float t = normalizedTime *curveLenghtInSeconds;
curveValue = aniCurve.Evaluate(t) *scale;
tempVector.y = curveValue;
transform.position = tempVector;
}
}
Add the script to cube, edit curve in inspector, hit play, use normalizedTime slider in inspector to see curveValue changinh. Script is for testing different curves at runtime, and you can tweak curve and scale and use curveValue for whatever you wish. I just added it to transform.position.y to get some visual representation.