How to activate and deactivate an enemy Rigidbody,

Hello! I am working on an FPS in which I wish to have a melee weapon that can either damage or knock back enemies on an attack and an alt attack. Currently both of these attacks work perfectly. Unfortunately the enemy having a rigidbody on it means every projectile or object that hits them causes them to be knocked back. Which I do not want. Is there any way to activate or deactivate the enemies Rigidbody when it is near the player? I tried attaching a trigger area to the knock back melee weapon that attempts to access the enemies Rigidbody when they enter it, but can’t seem to get it to work! Any advice on how to impliment this idea, or a better work around would be greatly appreaciated. :slight_smile: ,

Create a public game object for the enemy, and public Rigdbody rig, and script it with the following line of code:

public GameObject enemie;
public Rigdbody rig;

void Start(){

rig = GetComponent<Rigdbody>();

}

void Update(){
//add to your script

enemie.rig.enabled(false);

}

try this, but I haven’t tested it

For some reason when the enemy and its bullet are set to turn off the bullet keeps shooting in the direction it was when it was iMessage App off, how can I make it so that the bullet stops shooting at the given time. The first script is the shooting script and the second is turning it on/off.

Tag your enemy as Enemy (Create new tag if you dont have one). Have an empty child GameObject of the meelee weapon and position it in the tip of the weapon. Rename it to something like Detector. Now, add a sphere collider component to the object, and be sure to mark Is Trigger. On that gameobject, create and add a script that goes -

using System;
using System.Collections;
using UnityEngine;

namespace Game
{
class Detector : MonoBehaviour
{
private bool enemyDetected;

void OnTriggerEnter(Collision col)
{
if (col.gameObject.tag == "Enemy")
{
enemyDetected = true;

col.gameObject.GetComponent<Rigidbody>().enabled = false;
}
}

void OnTriggerExit(Collision col)
{
if (col.gameObject.tag == "Enemy")
{
enemyDetected = false;
col.gameObject.GetComponent<Rigidbody>().enabled = true;
}
}
}
}

Basically you can enable and disable Rigidbodies or any other component by

enemyRigidbody.enabled = false;