Knockback not working properly
Asked Answered
A

2

0

I am making a 3D game and have added a feature that if the Bot collides with a specific object, it gets knocked back.

I am using a NavMeshAgent for the bot and this goes against the Rigidbody so I disable one and enable the other for the knockback. the problem is that the bot doesn’t get knocked back in the right direction (the direction of what it collided from) and it is also inconsistent with how far it gets knocked back.

This is the script for it:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
  
public class KnockBack : MonoBehaviour
{
    private NavMeshAgent nma = null;
    private Rigidbody rb;
  
    public bool applyKnockBack;
  
    void Start()
    {
        nma = GetComponentInParent<NavMeshAgent>();
        rb = GetComponent<Rigidbody>();
    }
  
    void Update()
    {
        if(applyKnockBack)
        {
            StartCoroutine(KnockBackCo());
            applyKnockBack = false;
        }
    }
  
    void OnCollisionEnter(Collision col)
    {
        if (col.gameObject.tag == "Finish" || col.gameObject.tag == "Player")
        {
            StartCoroutine(KnockBackCo());
        }
    }
  
    IEnumerator KnockBackCo()
    {
        nma.enabled = false;
        rb.isKinematic = false;
  
        rb.velocity = Vector3.forward * -15;
  
        yield return new WaitForSeconds(0.5f);
  
        nma.enabled = true;
        rb.isKinematic = true;
    }
}
Abacus answered 7/7 at 14:38 Comment(0)
B
0

You’re setting a Rigidbody’s velocity to negative 15 in forward direction, resulting in a backwards direction in world space.

When the collision occurs. Store the knockback direction by taking target position and subtract origin position and normalize it.

Vector3 knockbackDirection = (gettingKnockedBacked.transform.positon - theKnockbacker).normalized;
  
//In coroutine
rb.velocity = knockbackDirection * knockbackForce;
Blacktop answered 7/7 at 14:38 Comment(1)

Perfect! thanks for the help. such a dumb mistake i made too haha.

Abacus
P
0

If someone else ends up here, I’ve read rigibodies and navmeshagents aren’t playing well together, so here’s how I did this (note that I’m a noob at C#).

//Applies knockback instantly

[SerializeField] float appliedForce = 10.0f;

NavMeshAgent agent;
Transform player;

void Awake()
{
    agent = transform.parent.GetComponentInParent<NavMeshAgent>();
    player = FindObjectOfType<PlayerHealth>().transform;
}
void Start()
{
    Vector3 _direction = transform.position - player.position; //Get direction of the knockback, opposite of source (here it's my player)
    agent.velocity = Vector3.zero; //set the navmeshagent velocity to 0
    agent.velocity = _direction * appliedForce; //add velocity to the navmeshagent with the direction
    Destroy(gameObject); //I'm using an approach where onHit I'm instantiating a prefab on the Enemy which handle the effect
} 
Pentagrid answered 6/7 at 13:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.