#SmashBros KnockBack and Health
1 messages · Page 1 of 1 (latest)
[]cb
Use codeblocks to send code in a message!
To make a codeblock, surround your code with ```
To use C# syntax highlighting add cs after the three back ticks.
For example:
```cs
Console.WriteLine("Hello World");
```
Produces:
Console.WriteLine("Hello World");
To send lengthy code, paste it into https://paste.myst.rs/ and send the link of the paste into chat.
public class BasicEnemy : MonoBehaviour
{
Throwing throwing;
[SerializeField] GameObject Player;
[Header("Stats")]
public float health;
public Rigidbody enemy;
private float power;
private float decrease;
public bool hitHard;
Vector3 Grav;
private bool fastFall;
public float duration;
public float elapsedTime;
private void Awake()
{
throwing = Player.GetComponent<Throwing>();
Debug.Log("Awake");
}
private void Start()
{
enemy = GetComponent<Rigidbody>();
elapsedTime = 5f;
}
public void TakeDamage(float damage)
{
health -= damage;
power = damage;
elapsedTime = 0f;
}
public void KnockBack()
{
enemy.AddForce((power) * ((health + 1f)) * (throwing.fixedDirection), ForceMode.Impulse);
enemy.AddForce(transform.up * 5f, ForceMode.Impulse);
Debug.Log("KnockBack Working");
SlowDown();
}
public void Update()
{
}
IEnumerator SlowDown()
{
if(hitHard)
{
var curSpeed = GetComponent<Rigidbody>().velocity.magnitude;
var newSpeed = curSpeed - (1.5f * (health/10)) * Time.deltaTime;
GetComponent<Rigidbody>().velocity = GetComponent<Rigidbody>().velocity.normalized * newSpeed;
yield return new WaitForSeconds(0.1f);
}
if (elapsedTime > duration)
{
hitHard = false;
}
else
{
hitHard = true;
}
elapsedTime += Time.deltaTime;
Debug.Log("Slowed");
}
}
@open bane thanks btw
u should save the Rigidbody in a variable and set it in awake
private Rigidbody rb;
private void Awake()
{
rb = GetComponent<Rigidbody>();
}```
also u need to start the coroutine with `StartCoroutine(SlowDown());` call
oh ok where should i call it, in knockback()?