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 goat3 · May 27, 2013 at 08:34 PM · enemy

Enemy Death and Killing Script

I am working on a side scroller game. you can run around, jump, pick up crates, and stuff like that. What I am having trouble on is making enemies. I want it to be kinda like Braid, or Mario, enemies go back and forth and if you get hit by one you die, but if you jump on it's head it dies. I have the AI enemy script, but I can't make it die, or kill you. PLEASE HELP! I prefer JavaScript, but if you can do it in C# or any other kind that is good too.

Here's my enemy AI script

 var pointB : Vector3;
  
 function Start () {
     var pointA = transform.position;
     while (true) {
         yield MoveObject(transform, pointA, pointB, 3.0);
         yield MoveObject(transform, pointB, pointA, 3.0);
     }
 }
  
 function MoveObject (thisTransform : Transform, startPos : Vector3, endPos : Vector3, time : float) {
     var i = 0.0;
     var rate = 1.0/time;
     while (i < 1.0) {
         i += Time.deltaTime * rate;
         thisTransform.position = Vector3.Lerp(startPos, endPos, i);
         yield; 
     }
 }
 
 
 and here is my character respawn script
 
 static var dead = false;
 
 function Update()
 {    
     //if you die you respawn in same level
     if(dead)
     {
         Application.LoadLevel(Application.loadedLevel);
     }
     
     //if you fall out of level you respawn
     if(transform.position.y < -25)
     {
         Application.LoadLevel(Application.loadedLevel);
     }
 }
 
 function OnCollisionEnter(hit : Collision)
 {
     //if you hit the enemy you die
     if(hit.gameObject.tag == ("Enemy"))
     {
         dead = true;
     }
 }
Comment
Add comment · Show 3
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 goat3 · May 27, 2013 at 08:32 PM 0
Share

Sorry about the weird script thing, I'm new to unity answers.

avatar image robertbu · May 27, 2013 at 08:40 PM 0
Share

To format code, select the code and use the 101/010 button. I've fixed it for you this time.

avatar image darthbator · May 28, 2013 at 09:38 PM 0
Share

What does happen? How about throwing some Debug.Log functions in there? Looks like you're checking the collision for the death event correctly.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by llSalvationll · May 28, 2013 at 09:49 PM

All you should need to do is test to see if the player's Y value is greater than the enemy's current Y value + the object's height. If the player's Y is greater than that value(or nearly so) then the enemy dies because the player is above the enemy. If not, the player dies.

Comment
Add comment · Show 2 · 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 goat3 · May 30, 2013 at 09:34 PM 0
Share

Thanks but even if my character hits the enemy, he won't die??

avatar image llSalvationll · May 30, 2013 at 09:44 PM 1
Share

What part of the death process is broken then - is the collision being detected between enemy & player? Throw a debug/print statement in your OnCollisionEnter function to know for sure. If not, make sure the spawned enemies are tagged as "Enemy" and that the objects you want to collide meet the requirements for collision to take place - i.e. rigidbody's and/or colliders, etc. Also make sure that you don't have 'Is Trigger' checked since you're using OnCollisionEnter.

avatar image
0

Answer by goat3 · May 30, 2013 at 10:21 PM

Thanks a lot, but right now even if the player hits the enemy he won't die

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 oblivion modder · Mar 18, 2015 at 05:37 PM

first attach this script to your enemy's collider and make sure it is called Enemy1.

  var fall :boolean;
   var stomp : boolean;
 
 function Update () {
 if(stomp){
 transform.position.z = 4;
 transform.localScale.y /= 2.00 ;
 fall = true;
 stomp = true;
 }
 if (fall){
 transform.position.y -= 0.10;
 }
 if (transform.position.y<-25){
 Destroy(gameObject);
 }
 }
 
 function OnTriggerEnter (other : Collider) {
 if (!stomp){
 if (other.tag == "Player"){
 Destroy(other.gameObject);
   
 }
 }
 }


Next create a collider that is called stomp and attach it above of your enemy and make sure it is a sub-object of your enemy. Add this script to it.

function OnTriggerEnter (Player : Collider){ transform.root.gameObject.GetComponent(Enemy1).stomp = true; gameObject.GetComponent(Enemy1).stomp=true; gameObject.GetComponent(Enemy1).fall=true;

}

that should work

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

16 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

Related Questions

Multiple Cars not working 1 Answer

Why is my enemy stopping before it reaches the player? 1 Answer

Instantiate with Prefabs 2 Answers

How do I stop a function from executing? 1 Answer

Trap Door Question 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