#SmashBros KnockBack and Health

1 messages · Page 1 of 1 (latest)

inner mica
#

the Coroutine that limits knock back i dont think was implemented correctly but maybe there is a simpler way

open bane
#

[]cb

brave owlBOT
#

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.

inner mica
#
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

open bane
#

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
inner mica
#

oh ok where should i call it, in knockback()?