Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 RjToni · Sep 17, 2012 at 05:11 AM · collidercontrollercolliderhit

How can I use OnControllerColliderHit to hit ONCE?

Hey Guys, so, I'm having problems to use collisions... anyway, let me explain:

I have a enemy ninja robot, with a sword, that will engage to attack the player, if comes close enough. I've attached a tiny sphere collider in the sword (the AI is using Character Controller), and I've attached a script to the player, with the function oncontrollercollider hit, to check if the sphere collides to the player, the player loses 1 point. But, when the sphere collides, he is just taking damage while is on collider. I mean, in a swing, he takes 10 hits or more.... It's not the deal....

Anyway, is there a way to make that check happens just once per time?

I've tryed OnCollisionEnter, too many times, but I've got no luck.

I'll appreciate any help. Thank you!

That's the script that checks the collision:

 var PlayerLife = 3;
 
 
 function OnControllerColliderHit(hit: ControllerColliderHit){
 if(hit.gameObject.name == "attackSphere"){
 
 PlayerLife -= 1;
 }
 }
 
 function Update () {
 
 print ("Health :" +PlayerLife);
 }

 
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
1

Answer by liszto · Sep 17, 2012 at 05:25 AM

You can use a boolean to check this like that :

 function OnControllerColliderHit(hit: ControllerColliderHit){
 if(hit.gameObject.name == "attackSphere" && !m_isHit){
 
 m_isHit = true;
 PlayerLife -= 1;
 }
 }

Change that value when you need your player take damage again

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 RjToni · Sep 17, 2012 at 05:40 AM 0
Share

Well, looks like the collision is more smooth, I mean, I don't have 10 hits, but sometimes I have 2, sometimes I have 1. That's the script so far:

var PlayerLife = 3; private var m_isHit : boolean = false;

function OnControllerColliderHit(hit: ControllerColliderHit){ if(hit.gameObject.name == "attackSphere" && !m_isHit){ m_isHit = true; PlayerLife -= 1; } else { m_isHit = false; }

}

function Update () {

print ("Health :" +PlayerLife); }

avatar image
0

Answer by RjToni · Sep 17, 2012 at 05:59 PM

Another issue what i've got. The collision stops to work after some hits. for example: after 30 hits, the collision doesn't work anymore, and this is relative. 30 hits, 20 hits, etc.

Can anybody help me? :/

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

11 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Using different paricle emitters depending on the tag of the object raycast hits? 1 Answer

Swapping 2 objects positions when one is dragged into the other 1 Answer

Bounds.Contains not working properly 3 Answers

I have an NPC that follows the player around. I want it to keep track and update 'Cover Positions' if things get bad. 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