Home > Software design >  Orienting an object to point along an animation curve
Orienting an object to point along an animation curve

Time:05-19

Working in Unity 2020.2.2f1, I have a modeled arrow following an animation curve. Is there a method to have the arrow pointing along the curve as it follows the path?

CodePudding user response:

Have you tried to put another Empty object in front of the arrow , as well animated on the same path. After use " public void LookAt(Transform target); "

public Transform target;

void Update()
{
    // Rotate the camera every frame so it keeps looking at the target
    transform.LookAt(target);

}

CodePudding user response:

Well, I've come up with a solution, if not the solution. My thanks to Daniel Pirvu for putting me on the right track. I found that it wasn't necessary, as per Daniel's suggestion, to create a new empty object to look at; instead I used the destination point/object as the lookAt target. I actually found that using Quaternion.LookRotation() works a bit more smoothly.

   void doCurve() {
    jumpTimer  = Time.deltaTime;
    if (jumpTimer > lerpTime) {
        jumpTimer = lerpTime;
        jump = false;
    }
    float lerpRatio = jumpTimer / lerpTime;
    Vector3 positionOffset = lerpCurve.Evaluate(lerpRatio) * lerpOffset;
    transform.position = Vector3.Lerp(targetA.position, targetB.position, lerpRatio)   positionOffset;
    
    // lookAt method
    //transform.LookAt(targetB.position);

    // LookRotation method
    Vector3 directionToDest = targetB.position - transform.position;
    transform.rotation = Quaternion.LookRotation(directionToDest);
}

with targetB cast as a Transform.

This does what I was looking for, though I don't know how well it'd work with, say, a car moving along a curvy path.

  • Related