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 jammachiq · Dec 07, 2013 at 07:54 AM · playerenemyattackhealth

How to make my player not lose health when attacking the enemy?

I feel like this is basic problem but I have set up and enemy (sawblade) that upon colliding with the player will take away 10 health points. However, my player has a slide animation that i am using as a way to attack the sawblade and destroy it. The player slides and hits the enemy destroying it but my problem is that upon doing this the player itself loses health. Below I will place my script from the Sawblade script that I have which indicates that when the player collides with the sawblade the player gets ten points taken away (Take Damage(10)). I thought that if I just did an else statement the sawblade would cancel damage taken when the player collides while sliding but that is not the case, what can i do to fix this?

void OnTriggerEnter(Collider c) {

 if(c.tag=="Player"){
 GameObject healthGameObject=GameObject.Find("Health");
 healthGameObject.GetComponent<Health>().TakeDamage(10);
 }else{
 if (GameObject.Find("Player").GetComponent<PlayerController>().sliding){
     GameObject healthGameObject=GameObject.Find("Health");
     healthGameObject.GetComponent<Health>().TakeDamage(0);
 }
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
2

Answer by EnglishMuffin123 · Dec 07, 2013 at 08:15 AM

Ok, so what you're saying in your code is that if the colliding object has the tag of player, take 10 damage, but if the colliding object's tag isn't player, take 0 damage. Since your player's tag is player whether it's sliding or not, this won't change anything. Try this:

 bool isSliding = false;
 
 void OnTriggerEnter(Collider c) {
 
 if(c.tag=="Player"){
     if(!isSliding) {
         GameObject healthGameObject=GameObject.Find("Health");
         healthGameObject.GetComponent<Health>().TakeDamage(10);
     }


You just have to set the isSliding variable to true every time you slide and you won't take any damage.

Comment
Add comment · Show 10 · 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 jammachiq · Dec 08, 2013 at 09:12 PM 0
Share

AWESO$$anonymous$$E!!! THIS WOR$$anonymous$$ED!! but i had to set issliding=true only because of the if(!issliding)

Thank you!!! :D

avatar image jammachiq · Dec 08, 2013 at 09:14 PM 0
Share

wait no i got excited too quickly. now the health does not go down at all.

avatar image wly_cdgr · Dec 08, 2013 at 09:19 PM 0
Share

set isSliding to true when the player starts sliding and set it back to false when the slide ends

avatar image jammachiq · Dec 08, 2013 at 09:28 PM 0
Share

Did I mention that I am very novice with scripting? i know that I should take courses on this but im a design student and just needs basic stuff for a final project. how would I set it back to false?

avatar image Commander Quackers · Dec 08, 2013 at 09:32 PM 0
Share

you can use else

Show more comments
avatar image
0

Answer by jammachiq · Dec 13, 2013 at 11:43 PM

public class SawBlade : MonoBehaviour {

// Update is called once per frame void Update () {

 //RBG (XYZ or Z = VECTOR3)
 transform.Rotate (Vector3.forward * speed * Time.deltaTime, Space.World);

}

void OnTriggerEnter(Collider c) {

 if(c.tag="Player"){
    PlayerControllerScript controller = c.gameObject.GetComponent<PlayerControllerScript>();
    if(!controller.isSliding){
      controller.isSliding=false;
      GameObject healthGameObject=GameObject.Find ("Health");
      healthGameObject.GetComponent<Health>().TakeDamage(10);
    }else{
      if(controller.isSliding){
       controller.isSliding=true;
      GameObject healthGameObject=GameObject.Find("Health");
      healthGameObject.GetComponent<Health>().TakeDamage(0);
    }
 }

}

yep this worked!

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

19 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

Related Questions

I use this script, but the enemy lose health if i don´t target him. 1 Answer

I want to attack me enemy and decrease their health, but t won't work, what am I doing wrong with my script? 4 Answers

How to stop enemy within certain distance of player? 2 Answers

Player Character Health 2 Answers

Enemy player is not attacking MC 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