Lerp - "Cache" Player Location as well

I’m trying to understand why we need to “cache” the Crystal location with “set” but not the player location like here.

Is it safe to assume we don’t cache the player because it’s dynamic and moves? If I try the above blueprint - the crystal will “lerp” to the player location but with a slight offset (like a ghost position) - I’m guessing it’s the player position when it made the collision, as that’s when we set the location.

How should I decide when it’s appropriate to “set/get” a variable and when to just get the Actor location/rotation/etc ?

I think the reason we need to set the lerp start location is because we don’t it to change it in the lerp every time the timeline changes the actor’s location.
We don’t need to cache the player’s location for the reason you mentioned. We don’t want the crystal to move toward where the player was when the collision first happened. We want the crystal to move toward where the player is in that given moment.
If we don’t cache the crystal, the transition might now be smooth because the location will be continuously updating. I haven’t tried it, but I expect the crystal would move much quicker and might not look as nice.
As far as when is it appropriate to set a variable goes. I’d say it depends on the use case. When doing a lerp, you will likely want the start location fixed no matter what. But the destination location might not be. Let’s say you have something firing a missile. You might not want the missile following the target, so in that case you would cache the target’s location.

1 Like

Thanks, @TastySnak for the response! I did some more testing with that blueprint and compared it to some of my Unity code in similar situations, and I think I have a better understanding of the get/set principle in Blueprints.

1 Like

The get/set principle gets hidden a bit in Unity because of how you can setup member variables/properties in C# whereas Blueprints and C++ are a bit more explicit.

1 Like

Thanks for the reply, @Tuomo_T. The way I think about this get/set in blueprints is how I would declare a variable at the top of the class and would cache its current value in the “Start function” (or Awake) in Unity.

As for “Get Actor Location” I would imagine it is the same thing as “myGameobject.transform” in C#. Am I correct in making these comparisons between unity c# and blueprints?

Yup, you’ve got it!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms