Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 Zuon94 · Sep 01, 2014 at 08:34 AM · collisionrigidbodydestroyenemy

Destroy Enemy Rigidbody after Jumping on Head?

I'm making a 2.5D platformer, and before, I had everything working great, except the enemies had no physics at all. So I added a rigidbody to them so they could stand and move on the same platforms I'm on. What I want to do though, is make it so that when I kill the enemy, the rigidbody will disappear so its body can fall off the screen. I feel like I've gotten close SO many times, but I've screwed around with the code so much, I give up on finding the fix by myself. How do I fix what I have broken? I would also like to know how to flip the enemy's texture depending on the direction he's facing, if anyone can look at that part of the code and give me pointers. The method I tried didn't work. My enemy is a textured quad with a box collider and my player is a textured plane with a modified first person controller, both rotated to face the camera, and locked on the Z axis. They are tagged "Enemy" and "Player" respectively. They have prefabs, especially so there can be duplicates of the enemy on the screen.

 #pragma strict
 private var fall : boolean;
 var Player : GameObject;
 var spawnPoint : PlayerRespawn;
 var stomp : boolean;
 var stompc : AudioClip;
 var X : float;
 var rbdy : GameObject;
 
 function Start () {
 var spawnPoint = Player.GetComponent("PlayerRespawn");
 //var enemy = Enemy.getComponent(Rigidbody);
 X = transform.localScale.x;
 var rbdy = GameObject.Find("Skater Kid 1").GetComponent(Rigidbody);
 }
 
 function Update () {
 if(stomp){
 //if(collision.rigidbody){
 //Destroy(enemy.GetComponent(Rigidbody));
 //}
 transform.position.z = 4;
 transform.localScale.y /= 2;
 fall = true;
 gameObject.GetComponent(PlatformMovement).step = 0.0;
 stomp = false;
 }
 if(rigidbody.velocity .magnitude  > 0){
 transform.localScale.x = X;
 }
 if(rigidbody.velocity .magnitude  < 0){
 transform.localScale.x = -X;
 }
 if(fall){
 transform.position.y -= 0.05;
 }
 if(transform.position.y < -25){
 Destroy(gameObject);
 }
 }
 function OnTriggerEnter(other : Collider){
 if(stomp){
 if(rbdy.CompareTag ("Enemy")){
 rbdy.Destroy(rbdy.rigidbody);
 }
 if(!stomp){
 if(other.tag == "Player"){
 spawnPoint.death = true;
 
 //var P : GameObject = Instantiate(Player, spawnPoint.position, Quaternion.identity);
 //var sf = Camera.main.GetComponent(smoothfollow2);
 //sf.target = P.transform;
 }
 }
 }
 }

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 chaozz · Sep 01, 2014 at 11:11 AM

Why not disable it instead of destroying it?

 this.GetComponent<Rigidbody>().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
avatar image
0

Answer by Zuon94 · Sep 01, 2014 at 05:53 PM

I solved it by putting "rigidbody.detectCollisions" in the top of the update function and making small adjustments to the rest of the code. Here's what I changed in "function.start":

 var rbdy = GameObject.FindGameObjectsWithTag("Enemy");

And here's the rest of the code.

 if(stomp){
 //if(other.tag == "Player"){
 rbdy.rigidbody.detectCollisions = false;
 //}
 transform.position.z = 4;
 transform.localScale.y /= 2;
 fall = true;
 gameObject.GetComponent(PlatformMovement).step = 0.0;
 stomp = false;
 }
 if(rigidbody.velocity .magnitude  > 0){
 transform.localScale.x = X;
 }
 if(rigidbody.velocity .magnitude  < 0){
 transform.localScale.x = -X;
 }
 if(fall){
 transform.position.y -= 0.05;
 }
 if(transform.position.y < -25){
 Destroy(gameObject);
 }
 }
 function OnTriggerEnter(other : Collider){
 if(!stomp){
 if(other.tag == "Player"){
 spawnPoint.death = true;
 }
Comment
Add comment · Show 1 · 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 Zuon94 · Sep 01, 2014 at 06:02 PM 0
Share

Wait, scratch that. The errors stopped, but it doesn't work the way I want it to. Running into the enemy no longer sends the player to the respawn position - it sometimes kills the enemy in the process, and after killing an enemy, if there's another one in the level, he just dies when he runs into you. I also think I messed up my colliders, because now there's a 50 percent chance of simply standing on the enemy when jumping on him. And the last problem I have is after killing an enemy, sometimes the player will start sliding uncontrollably to the side. I think I really screwed things up.

avatar image
0

Answer by Sabby123890 · May 18, 2015 at 10:22 AM

Make a different GameObject and merge it with the Player. Place it in the bottom. Name it feet and drag this Script(JavaScript) on the enemy :- Don't forget to add pragma strict to the script


function OnCollisionEnter (col : Collision) { if(col.gameObject.name == "feet") { Destroy(gameObject); } }_

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Why doesn't this script work?? Please help.. 1 Answer

How to make an enemy chase player, using collisions 2 Answers

Enemy Cube going through walls. 1 Answer

Enemy death help 1 Answer

Spawner continuing after boss dies 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