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 15, 2013 at 07:39 PM · collisionfpsenemydamage

Enemy deals damage to player on contact

Hey I want to know how to make it so when my enemy (cube) collides with my player (tag: Player) it deals 10 damage. This is my player code please help

 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);
     }
 }

I have this but it doesn't seem to work: function OnCollisionEnter(collision : Collision){

     if(collision.CompareTag("Player")){
 
         //Reduce global health var
         curHealth =- 10;
 
     }
 
 }

I want the enemies to be kind of like in this game:

http://www.twiik.net/content/twiikashoot

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 Rasheedh20 · Feb 19, 2014 at 05:24 PM

Have you given the cube a tag such as enemy to detect a collision?

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 AnXgotta · Feb 19, 2014 at 05:38 PM

At first glance, I would say that checking if a "Player" has collided with your player in your OnCollisionEnter(...) method isn't what you want.

Tag your enemies with "Enemy" and check for that tag in your player's OnCollisionEnter(...) method.

 function OnCollisionEnter(collision : Collision){    
     if(collision.gameObject.tag == "Enemy")){         
         //Reduce global health var
         curHealth =- 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

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

Enemy Collision Wont Work! PLEASE HELP 0 Answers

Enemy death help 1 Answer

Damage problem 4 Answers

Bullet not destroying on collision 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