Simplified version of this is: I spawn an object with a fixed velocity and it should move like the blue line. How much it gets away from the red line should be same for both left and right (“r” in the image). How can I do that by changing the velocity?
Spline as the solution won’t work because I need to move the object with velocity and, how far the object can go depends.
I love code-able motion like this… so much fun to script out!
This “buttonhole” kinda motion could just be parametrically expressed with sine and cosine, driven with speed of motion and rate of angle change.
Check it (full package attached):
using UnityEngine;
// @kurtdekker:
// stick me on a visible object, watch me go fishy fish forward
// we consider our original transform.forward to be +Z forward.
// This is also called a buttonholing motion.
public class TrigFish : MonoBehaviour
{
// how quickly I move forward locally along the curve
public float LinearSpeed = 5;
// how quickly I change my angle
public float AngularSpeed = 90;
// TODO: change this in real time to steer me around the world
public float ForwardAngle = 0;
void StepRotateAndDrivePositionFromPhase()
{
// forward march!
transform.position += transform.right * (LinearSpeed * Time.deltaTime);
float angle = phase % 360;
// are we zigging or are we zagging??
if (angle < 180)
{
angle = 360 - angle;
}
// this lets you steer it
angle += ForwardAngle;
// this makes it rotate around Y;
// if you want around Z, put angle in the 3rd spot
// and use transform.up for the above
transform.rotation = Quaternion.Euler( 0, angle, 0);
}
// this is the accumulator that tracks your cycle around one full buttonhole
float phase;
void Update ()
{
StepRotateAndDrivePositionFromPhase();
phase += AngularSpeed * Time.deltaTime;
}
}