Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by $$anonymous$$ · Feb 03, 2016 at 06:35 PM · triggerkill player

Platform kill player on contact

How can i make this script to set CurrentHealth to 0 or lower when entering a certain trigger? I have made a platform that is moving and i want to detect if that platform hits the player and not detect if the player is on top of it.

 #pragma strict
 
 var lastPositionY : float = 0f;
 var fallDistance : float = 0f;
 var player : Transform;
 var maxDistance : float = 3f;
 var healthTimer : float = 0f;
 var regenTime : float = 200f;
 var regen : float = 1f;
 var healthMax : float = 20f;
 var resX : float = 0f;
 var resY : float = 1f;
 var resZ : float = 0f;
 var maxFall : float = 500f;
 var timerSec : float = 0f;
 var manualReset : KeyCode;
 var currentHealth : float = 20.0f;
 
 private var controller : CharacterController;
 
 function Start()
 {
     controller = GameObject.Find("FPSController").GetComponent(CharacterController);
 }
      
 function Update ()
 {   
     if(lastPositionY > player.transform.position.y)
     {
         fallDistance += lastPositionY - player.transform.position.y;
     }
     
     lastPositionY = player.transform.position.y;
     
     if(fallDistance >= maxDistance && controller.isGrounded)
     {
         currentHealth -= 1;
         fallDistance -= 1;
         print("MainScript.js: Fall damage taken. HP left: " + currentHealth + " healthTimer: " + healthTimer);
     }
      
     if(fallDistance <= maxDistance && controller.isGrounded)
     {
         ApplyNormal();
     }
 
     if(currentHealth <= 0)
     {
         transform.position = new Vector3(resX, resY, resZ);
         lastPositionY = 0f;
         fallDistance = 0;
         currentHealth = 20.0;
         print("MainScript.js: Player is dead");
     }
 
     if(currentHealth < healthMax) {
         healthTimer += 1;
     }
 
     if(healthTimer >= regenTime && currentHealth <= (healthMax - 1))
     {
         currentHealth += regen;
         healthTimer -= regenTime;
         print("MainScript.js: Health is below " + healthMax + ". Regen from: " + (currentHealth - regen) + " Regen to: " + currentHealth);
     }
 
     if(fallDistance >= maxFall)
     {
         transform.position = new Vector3(resX, resY, resZ);
         print("MainScript.js: Player fell out of map (Distance: " + fallDistance + ")");
         ApplyNormal();
     }
 
     if(Input.GetKeyDown(manualReset))
     {
         ApplyNormal();
         transform.position = new Vector3(resX, resY, resZ);
         currentHealth = 20;
         healthTimer = 0;
         print("MainScript.js: key '" + manualReset + "' pressed, player reset");
     }
 
 }
 
 function ApplyNormal()
 {
         fallDistance = 0;
         lastPositionY = 0;
 }
 
 function OnGUI()
 {
     GUI.Box(Rect(0, 0, 100, 20),"Health: " + currentHealth);
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Jhyatt · Feb 03, 2016 at 08:00 PM

You'll need a trigger positioned at the location you want your character to die if it enters.

These are the steps I would take to achieve this effect.

  1. Create a cube

  2. Check "Is Trigger" under the Box Collider properties.

  3. Position and scale the cube so that it is in the area you want your character to die if entered.

  4. Make the cube a child of your platform game object.

  5. Disable the mesh renderer of the cube.

  6. Create a new tag for the cube and call it something like "DeathTrigger"

Then apply this section of code to your character script:

 void OnTriggerEnter(Collider collider)
 {
     if (collider.tag == "DeathTrigger")
     {
         currentHealth = 0f;
     }
 }

(Take note that this code is C#. I don't know if it's exactly the same syntax for your JS code.)

I hope that helps you out.

EDIT:

I think this would be the syntax for JS. I'm not 100% certain on that though.

 function OnTriggerEnter (collider: Collider) {
     if (collider.tag == "DeathTrigger")
     {
         currentHealth = 0f;
     }
 }
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 $$anonymous$$ · Feb 04, 2016 at 07:59 AM 1
Share

Ty, That code works like a charm for me! Now im 1 step closer to get the scripts i need

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

43 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

Related Questions

How to make an object collide and trigger at the same time 1 Answer

About the door animation trigger 0 Answers

NavmeshDestination not changing. 0 Answers

Rotating Enemy when enters trigger area 1 Answer

Resetting my cannon fire to its original transform 0 Answers


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