maybe I’m missing something obvious?
But I’m looking to have my player get knocked back a little each time he fires a projectile.
Currently, no matter which forceMode I use, the knockback is instantaneous. I’m hoping to get it a little more smooth. I’ve also tried lerping the Vector3 and using a co-routine.
See the code below.
Actual knockback is in the Shoot() method.
Any ideas?
private void Update()
{
if (Input.GetMouseButtonDown(0) && Time.time - lastTimeStamp >= timeBetweenCasts && pieMenuScript.pieSelection == PieMenu.spellMenuSelection.None)
{
//reset the timer
lastTimeStamp = Time.time;
//TODO: Object pooling
RaycastOnMouseClick();
}
}
private void RaycastOnMouseClick()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit groundHit;
Debug.DrawRay(ray.origin, ray.direction * 100f, Color.red, 2);
if (Physics.Raycast(ray, out groundHit, 300f, layerMask))
{
mousePosition = groundHit.point + new Vector3(0f, spawnPoint.position.y, 0f); //adding the spawnPoint.position to this ensures that the projectiles stay parallel to the ground
direction = (mousePosition - spawnPoint.transform.position).normalized;
Shoot(groundHit);
}
}
void Shoot(RaycastHit hit)
{
if (ManaManager.instance.currentMana >= manaCost)
{
ManaManager.instance.ManaCost(manaCost);
manaImage.fillAmount -= manaCost / ManaManager.instance.maxMana;
Quaternion pointDirection = Quaternion.LookRotation(direction);
var projectileInstance = Instantiate(projectile, spawnPoint.transform.position, pointDirection).GetComponent<ShootProjectile>();
Instantiate(castParticle, spawnPoint.transform.position, pointDirection); //instantiate cast Particle effect
if (playerKnockback)
{
playerRigidbody.AddForce(-direction * playerKnockbackForce);
}
}
else
{
Debug.Log("Not Enough Mana");
}
}