• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by RoobotShepard · Nov 21, 2020 at 01:19 AM · getcomponent

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. :) ,

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Pokedlg3 · Nov 21, 2020 at 03:21 AM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Erickson76 · Nov 23, 2020 at 12:14 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Maggiethegsd · Nov 21, 2020 at 01:46 PM

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;

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

145 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Accessing other objects efficiently 1 Answer

Destroy(GetComponent(...)) not working 1 Answer

GetComponent(); in Awake cant be accessed in Update 1 Answer

BCW0012: WARNING: 'UnityEngine.MeshCollider.mesh' is obsolete. mesh has been replaced with sharedMesh and will be deprecated 1 Answer

Problem getting information out of colliding objects 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges