How to Lerp a moving rb2d
So I am trying to recreate this pattern for two projectiles in my game:
The way I am trying to do it is by instantiating the projectiles every second while the mouse0 button is being held down by the player, then check if the projectiles are null, if not, apply velocity to them and lerp them based on their transform.up, the issues I am having are:
1- The first projectiles that were shot stop lerping once others are instantiated 2- They simply stop moving and start glitching after a while
Here is the entire script for this:
[SerializeField] private Transform projectileSpawnL, projectileSpawnR;
[SerializeField] private GameObject projectilePrefab;
private GameObject projectileL, projectileR;
private Rigidbody2D projectileLB, projectileRB;
private float fireRate = 1f;
private void Update()
{
fireRate -= Time.deltaTime;
}
private void FixedUpdate()
{
if (Input.GetButton("Fire1") && fireRate <= 0)
{
Shoot();
fireRate = 1f;
}
if(projectileL != null && projectileR != null)
{
projectileR.transform.position = Vector2.Lerp(projectileR.transform.position, projectileR.transform.up, 0.01f);
projectileL.transform.position = Vector2.Lerp(projectileL.transform.position, -projectileL.transform.up, 0.01f);
projectileLB.velocity = projectileLB.transform.right * 2;
projectileRB.velocity = projectileRB.transform.right * 2;
}
}
private void Shoot()
{
projectileL = Instantiate(projectilePrefab, projectileSpawnL.position, projectileSpawnL.rotation);
projectileR = Instantiate(projectilePrefab, projectileSpawnR.position, projectileSpawnR.rotation);
projectileLB = projectileL.GetComponent<Rigidbody2D>();
projectileRB = projectileR.GetComponent<Rigidbody2D>();
}
Your answer
Follow this Question
Related Questions
Replace Vector3.Lerp with something equivalent for rigidbody2D !! 1 Answer
Trick to move a game object smoothly to left and right 0 Answers
Rigid body vibrating? 0 Answers
Bug - Able to pass through rigidbody/collider when using Lerp/SetPosition 0 Answers
LERPing Rigidbody2D still passing through collider 0 Answers