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 PwnsomeWin2 · Jun 26, 2012 at 02:24 PM · timerthread

How to make a spike?

I made a spike in my game... when the player touches it, I want the play to lose ten health. Then, if they are still touching it a second later, I want them to lose another ten health. Here's my code so far:

function OnTriggerCollider (player : Collision) { if (player.tag == "Player") { Player.gameObject.GetComponent(PlayerHealth).playerHealth -= 10;

 }

}

Comment
Add comment · Show 6
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 PwnsomeWin2 · Jun 26, 2012 at 02:43 PM 0
Share

The two collision functions and the second DecreaseHealth function aren't working.

avatar image PwnsomeWin2 · Jun 26, 2012 at 10:51 PM 0
Share

Still need help...

avatar image AlucardJay · Jun 27, 2012 at 12:54 PM 1
Share

this is a search of this 'site using your title as the search string ....

http://answers.unity3d.com/search.html?redirect=search%2Fsearch&q=How+to+make+a+timer

avatar image PwnsomeWin2 · Jun 27, 2012 at 01:09 PM 0
Share

Well, I have the timer done, I just need to figure out why the spike won't hurt the player.

avatar image Kiloblargh · Jun 27, 2012 at 05:05 PM 0
Share

Where did you get "OnTriggerCollider," anyway?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by GuyTidhar · Jun 26, 2012 at 02:32 PM

The function you made will not be called. You need to call unity's functions if you want this to work.

Do this for collision:

 function OnCollisionEnter (collInfo: Collision) 
 { 
      if (collInfo.collider.tag == "Player") 
      { 
          StartCoroutine("DecreaseHealth", collInfo.gameObject.GetComponent(PlayerHealth));
      }
 }
 function OnCollisionExit (collInfo: Collision) 
 { 
      if (collInfo.collider.tag == "Player") 
      { 
          StopCoroutine("DecreaseHealth");
      }
 }
 
 function DecreaseHealth(ph : PlayerHealth)
 {
    ph.playerHealth -= 10;
    yield WaitForSeconds(1f);
    ph.playerHealth -= 10;
 }

If you wish health to be reduced each second afterwards and not only the first, change the coroutine "DecreaseHealth" to this:

 function DecreaseHealth(ph : PlayerHealth)
 {
    var run : boolean = true;
    while ( run )
    {
        ph.playerHealth -= 10;
        yield WaitForSeconds(1f);
    }
 }

This will work if what you are testing is collision and not a trigger. A collision means two objects will not cross each other, while a trigger means the collision is noticed but the objects can still cross each other. If you need trigger functionality you will need to change the "OnCollisionEneter" and "OnCollisionExit" to the following:

 function OnTriggerEnter (other: Collider) 
 { 
      if (other.tag == "Player") 
      { 
          StartCoroutine("DecreaseHealth", collInfo.gameObject.GetComponent(PlayerHealth));
      }
 }
 function OnTriggerExit (other: Collider) 
 { 
      if (other.tag == "Player") 
      { 
          StopCoroutine("DecreaseHealth");
      }
 }
Comment
Add comment · Show 13 · 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 Berenger · Jun 26, 2012 at 02:50 PM 0
Share

PwnsomeWin : I used the collision script with the second DecreaseHealth function, but its still not working.

avatar image GuyTidhar · Jun 26, 2012 at 03:40 PM 0
Share

Do you have a character controller on the player? A rigidbody and a collider on the spike?

avatar image PwnsomeWin2 · Jun 26, 2012 at 08:40 PM 0
Share

Yup, still not working.

avatar image GuyTidhar · Jun 27, 2012 at 04:54 AM 0
Share

Read this page, and especially note the table at the bottom:

http://unity3d.com/support/documentation/Components/class-BoxCollider.html

avatar image PwnsomeWin2 · Jun 27, 2012 at 12:20 PM 0
Share

I read it, what now?

Show more comments

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

how do I make a Time Based Score System? 1 Answer

C# timers - SetSprite_INTERNAL can only be called from the main thread. 1 Answer

Disable the function timer? 1 Answer

Detecting exceptions outside of the main Unity thread 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