Enemy does not get Knocked back anymore

Great course so far by the way! I checked that they have the Force Receiver correctly set up. No matter what I put in the knockback value for attacks on the player, the enemy won’t get force applied. Player does get knocked back when Enemy attacks. Not sure what I am doing wrong.

Force Receiver script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class ForceReceiver : MonoBehaviour
{
    [SerializeField] private CharacterController controller;
    [SerializeField] private NavMeshAgent agent;
    [SerializeField] private float drag;
    private float verticalVelocity;
    private Vector3 impact;
    private Vector3 dampingVelocity;
    public Vector3 Movement => impact + Vector3.up * verticalVelocity; 

    private void Update(){
        if(controller.isGrounded && verticalVelocity<0f){
            verticalVelocity = Physics.gravity.y * Time.deltaTime;
        }else{
            verticalVelocity +=Physics.gravity.y * Time.deltaTime; 
        }

        impact = Vector3.SmoothDamp(impact,Vector3.zero, ref dampingVelocity, drag);
        if(agent != null){
            if (impact == Vector3.zero){
                agent.enabled = true;
            }
         }
    }

    public void AddForce(Vector3 force){
        impact += force;
        if(agent != null){
            agent.enabled = false;
        }
    }
}

UPDATE: Just tested out with a greater value and it does not work for the player either. So there might be something in the script.

Solved it! the drag value for the force receiver was set to 0 in the inspector. Set it to 0.3 and it is now working.

1 Like

Welcome to the community! :slight_smile:

Well done for finding the solution, sometimes writing the problem out can be a helpful way of working through it and finding the answer!

Well done sorting that!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms