- Home /
knockback 3d c#
hello im new but i nedd help with a script that make my player get more knockback if its on lower hp im still dont now how to get this kind of knockback here are my scripts c#
my player hp:
public class hpP1 : MonoBehaviour { public const int maxHealth = 100; public int currentHealth = maxHealth;
public void TakeDamage(int amount)
{
currentHealth -= amount;
if (currentHealth <= 0)
{
currentHealth = 0;
Debug.Log("Dead!");
}
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "bullet")
{
currentHealth -= 5;
transform.localScale += new Vector3(0.0018f, 0.0018f, 0.0018f);
Vector3 direction = (transform.position - col.transform.position).normalized;
}
}
}
and my bullet shooting script:
{
public Rigidbody projectile;
public float speed = 20;
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Rigidbody instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(speed, 0, 0));
}
}
}
its a 3d platformer like SSB but then with shooting so hopes somebody helps me with this
Answer by bliss-art · Dec 15, 2017 at 07:20 PM
I would keep a "knockbackForceMultiplier" value: 1 - (currentHealth / maxHealth). It's 0 when the agent is at full health and 1 when he's at 0 health. Then just use this value to scale some extra force that is added to your default knockback force.
Your answer
Follow this Question
Related Questions
Add force on the ball based in Mouse Drag 0 Answers
Knockback on player without having a rigidbody? 0 Answers
Need help with knockback in C# Please! 2 Answers
The random generated obstacles spawn outside the game platform 1 Answer
issue with the rigid body x axis ,addforce doesn't work as it should for x axis 1 Answer