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 Jeremias · Apr 02, 2013 at 09:05 PM · charactercontrolleroncontrollercolliderhit

Damage Player when this object collides the CharacterController?

Well lets say this script is attached to a box, that box is a child of my Enemy and has a box collider and a kinematic rigidbody because if not it mess up. Im trying to do that when this object collides with player charactercontroller the Player gets damaged. Here is what i tryed. Raycasting works but is not what im looking for, I also tryed OnCollisionEnter butdidnt worked.And i dont know if im using OnControllerColliderHit correctly. So can you give me any solution? Please!!

 var attack = 30;
 var canattack = 1;
 var cooldown : double = 2.2;
 var distance : double = 0.5;
 
 //function Update() {
 //    if(Enemy.GetComponent(EnemyAI).attackEnabled == 0) {
 //    var fwd = transform.TransformDirection (Vector3.forward);
 //    var hit : RaycastHit;
 //    if(canattack == 1) {
 //        if (Physics.Raycast (transform.position, fwd, hit, distance)) {
 //            if (hit.collider.gameObject.tag == "Player") {
 //                hit.collider.gameObject.GetComponent(PlayerHealth).damage += attack;
 //                print ("Ouch!That Hurts");
 //                canattack = 0;
 //                Attack();
 //            }
 //        }
 //    }
 //}
 //function Attack() {
 //    while(true) {
 //        if(canattack == 0) {    
 //            yield WaitForSeconds(cooldown);
 //            canattack = 1;
 //        }
 //        else {
 //            yield;
 //        }
 //    }
 //}
 
 //function OnCollisionEnter(collision : Collision) {
 //    print ("Collide!");
 //    if (collision.gameObject.tag == "Player") {
 //        collision.gameObject.GetComponent(PlayerHealth).damage += attack;     
 //    print ("Ouch!");
 //    }
 //}
 
 //function OnControllerColliderHit (hit : ControllerColliderHit) {
 //    if (hit.collider.tag == "Player") {
 //        hit.collider.GetComponent(PlayerHealth).damage += attack;
 //        print ("Ouch!");
 //     }
 //}
Comment
Add comment · Show 8
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 sdgd · Apr 02, 2013 at 09:12 PM 1
Share

try doing this way

script on box

 OnTriggerEnter(Collider Other){
     Player PlayerScript = Other.gameObject.getComponent("Player") as Player;
     PlayerScript.Health --;
 }

box has to have collider is trigger true

something like this sorry if here are typos but I've written it without unity's help

and OFC I write in C#

avatar image KMKxJOEY1 · Apr 02, 2013 at 09:48 PM 1
Share

that is for trigger colliders..

avatar image Jeremias · Apr 02, 2013 at 11:32 PM 0
Share

@sdgd Can you translate it to java so i can try it?

avatar image KMKxJOEY1 · Apr 03, 2013 at 12:18 AM 0
Share
 function OnTriggerEnter(Collider Other){
     var PlayerScript = Other.gameObject.getComponent("Player");
     PlayerScript.Health --;
 }

that is his in js

avatar image KMKxJOEY1 · Apr 03, 2013 at 03:19 PM 1
Share

Guys trigger enter is for trigger colliders $$anonymous$$y answer below is for actual collisions

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by KMKxJOEY1 · Apr 02, 2013 at 09:50 PM

 function OnCollisionEnter(collision : Collision) {
   print ("Collide!");
   if (collision.gameObject.tag == "Player") {
 //   collision.gameObject.GetComponent(PlayerHealth).damage += attack; 
  collision.gameObject.GetComponent(PlayerHealth).health -= attack;
   print ("Ouch!");
   }
 }

it looks like you were adding to the damage of the player health script

i added a line where it will subtract from the health instead

Comment
Add comment · Show 6 · 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 Jeremias · Apr 02, 2013 at 11:30 PM 0
Share

Sorry but my script is ok cause:

 curHealth = maxHealth - damage;
avatar image Jeremias · Apr 03, 2013 at 04:43 PM 0
Share

Thats what I tried already but doesnt works withcharacter controllers.

avatar image KMKxJOEY1 · Apr 04, 2013 at 01:16 AM 0
Share

it is printing collide right? and the tag on the player is a capitol P and correct spelling and such. maybe print the colliders name so you can check what it is hitting like this:

 print("Collide! with " + collision.gameObject.name);
avatar image Jeremias · Apr 04, 2013 at 03:50 AM 0
Share

I added that but it seems it doesnt collides with my character.

avatar image Jeremias · Apr 04, 2013 at 03:52 AM 0
Share

If i change the box to non-kinematic it works ok but after firsthit it goes flying away

Show more comments
avatar image
0

Answer by Jeremias · Apr 03, 2013 at 03:47 PM

There is no solution for this?

Comment
Add comment · Show 2 · 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 KMKxJOEY1 · Apr 04, 2013 at 01:15 AM 0
Share

be patient, dont post a new 'answer'. if you want to bump, post a comment, but that is after no activity for several days. remember, we all have lives too

avatar image Jeremias · Apr 04, 2013 at 01:54 AM 0
Share

Yes I $$anonymous$$now,Im Looking other questions too but none answer my questions.

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

13 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

Related Questions

How to to stop my box from moving when using OnControllerColliderHit 1 Answer

Character controller and colliders neither set off 'OnCollision' nor ' 'OnTrigger'; are ridigbodies mandatory? 1 Answer

OnControllerColliderHit Doesn't always get detected? 0 Answers

Character Controller problem 1 Answer

character collider colliding with another character collider 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