#EasyVector Lerping
1 messages · Page 1 of 1 (latest)
Why is vector3 lerp messy? Just curious
The way I learned, and this might just be a personal error, was to create a few variables that permanently lived in my variable declaration, which is turning into a giant list as it is.
Since the variables have to be referenced inside the Update method and not lose their values.
Do you have any example and comparison to what you did differently?
Or is the EZLerp just a class that is basically lerping for you to not have to duplicate that "giant" code to lerp?
sure.
The lerping implementation itself isn't giant, it is just adding to a giant list of other variables.
Just a second thought. I also had the issue, that I was a fan of one line animating stuff, so I created an extension for monobehaviours, that can just call Animate(from, to, time) etc etc and it will start its own coroutine in side that extension class.
public class PlayerShip : MonoBehaviour
{
// ... 25 other variables
// Knockback Lerp
private bool wasKnockedBack = false;
private float elapsedTime = 0f;
private float knockbackDuration = 0.3f;
private Vector3 knockbackStartPosition;
private Vector3 knockbackEndPosition;
// ... other methods...
// Collision Methods
void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.CompareTag("Enemy"))
{
ContactPoint2D[] contacts = new ContactPoint2D[collision.contactCount];
collision.GetContacts(contacts);
Vector2 angleOfContact = new Vector2(
contacts[0].point.x - playerData.positionOnMap.x,
contacts[0].point.y - playerData.positionOnMap.y
);
angleOfContact.Normalize();
Knockback(angleOfContact, collision.gameObject.GetComponent<EnemyShip>());
}
}
void KnockbackLerp()
{
elapsedTime += Time.deltaTime;
float percentageComplete = elapsedTime / knockbackDuration;
transform.position = Vector3.Lerp(knockbackStartPosition, knockbackEndPosition, percentageComplete);
if(percentageComplete >= 1.0f)
{
knockbackStartPosition = Vector3.zero;
wasKnockedBack = false;
elapsedTime = 0.0f;
}
}
void Knockback(Vector2 angleOfContact, EnemyShip enemyShip)
{
animator.SetTrigger("hit");
knockbackStartPosition = transform.position;
knockbackEndPosition = new Vector3(
transform.position.x - enemyShip.knockback * angleOfContact.x,
transform.position.y - enemyShip.knockback * angleOfContact.y
);
wasKnockedBack = true;
}
}
That's clever!
The whole script also checks for a list of coroutines running, so you dont start parallel animations. But for now, my companys devs love to use it so far 🙂
Noice. ^.^ Make sure you license that so you can get a patent remittance when you leave.
Depending on the size of the company.
Nah, if I am ever allowed to share it, i woudl make it open source 😄 its just clever combinatino of things that are there already 🙂
A computer program, as it were.
I have almost all of my Typescript/Javascript for unit testing under MIT.
buuut, this Lerping class is pretty nice because I can just pass a Vector or Transform for the endpoint and a duration, and let it run.
Pretty convenient, since I was wondering how I was going to implement enemy movement and with this I can just feed a list of waypoints and let it ride.