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 samaxi · Jun 23, 2013 at 12:42 AM · fpsaienemydamagehealth

Deal damage on collision

Hi, I want my game to work so if the enemy collides with the player the player loses 10 health but I don't know what to do and I've tried every tutorial out there. Please help I am desperate.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerVitals : MonoBehaviour {
     public GameObject guiObject;
     public int curHealth = 100;
     public int maxHealth = 100;
     public int recoveryAmount = 1;
     public float recoverySpeed = 3;
     public Color bloodColor = new Color(1.0f, 0.0f, 0.0f, 1.0f);
     public Texture2D bloodyScreen;
     public float fallDamageHeight = 8;
     public AudioClip fallDamage;
     
     [HideInInspector] public float percent;
     
     private float alpha;
     private float iv2;
     private float minimumHealth = 40;
     private float bmhRatio;
     private PlayerMovement pm;
     private CharacterController controller;
     
     void Start() {
         minimumHealth = 40;
         pm = GetComponent<PlayerMovement>();
         if(recoveryAmount != 0) {
             StartCoroutine(Regenerate(0));
         }
         controller = GetComponent<CharacterController>();
     }
     
     void OnControllerColliderHit(ControllerColliderHit hit) {
         Rigidbody rigid = hit.collider.attachedRigidbody;
         
         if(rigid == null || rigid.isKinematic || hit.moveDirection.y < -0.3f) {
             return;
         }
         
         Vector3 pushDir = new Vector3(hit.moveDirection.x, 0, hit.moveDirection.z);
         rigid.velocity = pushDir * controller.velocity.magnitude * pm.pushPower / rigid.mass;
     }
     
     void OnGUI() {
         GUI.color = new Color(bloodColor.r, bloodColor.g, bloodColor.b, alpha);
         GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), bloodyScreen);
     }
     
     void FixedUpdate() {
         alpha = Mathf.Lerp(alpha, 0.9f - (percent * 0.9f), Time.deltaTime * 7);
         curHealth = Mathf.Clamp(curHealth, 0, maxHealth);
         percent = (float)curHealth / (float)maxHealth;
         iv2 = pm.impactVelocity;
         if(curHealth <= 0) {
             Die();
         }
         Repud.GetChild(guiObject, "health_text").GetComponent<TextMesh>().text = curHealth.ToString() + "/" + maxHealth.ToString();
         Repud.GetChild(guiObject, "health_bar").SendMessage("UpdateSize", percent);
     
         bmhRatio = maxHealth / minimumHealth;
         
         if(pm.grounded && pm.floating && iv2 > 1) {
             SendMessage("FallAnimation", iv2);
         }
         
         if(pm.grounded && pm.floating && iv2 > fallDamageHeight) {
             FallDamage(((iv2 - (fallDamageHeight * 0.8f)) * ((iv2 - (fallDamageHeight * 0.8f)) * 0.5f)));
             iv2 = 0;
             pm.floating = false;
         }
     }
     
     public IEnumerator Regenerate(int amount) {
         curHealth += amount;
         yield return new WaitForSeconds(recoverySpeed);
         StartCoroutine(Regenerate(recoveryAmount));
     }
     
     private void FallDamage(float prc) {
         if((int)(prc) <= 0) {return;}
         ApplyDamage((int)prc);
         audio.clip = fallDamage;
         audio.volume = 0.02f;
         audio.Play();
     }
     public void ApplyDamage(float amount) {
         curHealth -= (int)amount;
     }
     
     public void Die() {
         Destroy(gameObject);
     }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by create3dgames · Jun 25, 2013 at 01:52 AM

This in JavaScript:

 var health : int = 100;
 
 function OnCollisionEnter (collision : Collision)
 {
    if (collision.tag == "Enemy")
    {
       health -= 10;
    }
 }
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 Mwester · Mar 16, 2017 at 09:12 AM

I know this is an old topic, but just in case anyone else is searching for a good answer to this, follow the answer by @create3dgames, and I would like to add right under the if statement

if(Health < 0 ) {

     Health = 0;
 }

This will prevent the health from going into negative numbers, which I doubt most people will want... Also, to be able to add enemy characters and have them be able to damage the player's health, I would change "var health : int = 100;" to "static public var health : int = 100;". This will allow you to call on the player's health in ANY other script in your project (I don't know if it works in csharp), as long as you call on it like "[name of the script here with var health in it, but remove the brackets].health", which is VERY useful when making a game.

Best of luck to anyone who struggles with this.

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

17 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

Related Questions

The enemy don´t lose health, but why??? 0 Answers

Ai that applies damage when in range? 1 Answer

Enemy deals damage to player on contact 2 Answers

FPS Problem Help! 0 Answers

Health and damage reciever 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