- Home /
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;
}
}
}
}
Answer by chaozz · Sep 01, 2014 at 11:11 AM
Why not disable it instead of destroying it?
this.GetComponent<Rigidbody>().enabled = false;
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;
}
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.
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); } }_
Your answer
Follow this Question
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