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 RoflJOE · Dec 05, 2010 at 04:33 PM · collisioncharactercontrollerdetectionball

Collision With Character Controller

Hey, Im new to unity but find it very interesting ^^

Ive bin struggeling with a problem for several hours now, and I can cleary see that i need some help with it.

I have a turret firing balls into my world, and when the balls hit my character(First Person Character Controller) i want the game to stop (gameover)

But i dont know how to do this.

Can anyone link me a tutorial or a script that could do this, thank you very much in advance ;)

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

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by RoflJOE · Dec 05, 2010 at 10:54 PM

Okay this seems to work..

function OnCollisionEnter (other : Collision) 
{ 
    if (other.gameObject.name == "FirstPersonController") { 
       print("Game Over");
   } 
}

Comment
Add comment · Show 3 · 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 Thet Naing Swe · Mar 28, 2011 at 07:38 AM 1
Share

OnCollisionEnter doesn't work on Character controller and it only works on Rigidbody

avatar image brimock · Dec 12, 2012 at 04:29 AM 0
Share

OnCollisionEnter does work on character controller. Character Controller inherits from Collider, that has OnCollisionEnter

avatar image dpk · May 28, 2013 at 06:32 PM 0
Share

It may appear to work if your object happens to have a Rigidbody, but if all it has is a Character controller OnCollisionEnter will not fire.

avatar image
0

Answer by Justin Warner · Dec 05, 2010 at 04:35 PM

Get the distance between the fire ball (Prefab?) and the player using Vector3.Distance(FIREBALL,PLAYER);

Then do an if statement to check if the fireball hit the player... If so, then start a new scene, where it shows they lost, or if you want to keep the background, tweak a pause game script... Search for one of those!

=)

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 RoflJOE · Dec 05, 2010 at 04:56 PM

I was thinking about: Can i somehow put a tag on the character controller and tell the ball script that when it collides with something that has that tag the game will pause or shift to another scene? ;) RoflJOE 58 secs ago

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 L5D7 · Mar 09, 2012 at 07:43 AM

I know this is old but I've had the same problem for a little while now and have come up with a very useful solution. I don't know how efficient it is, but it does work.

In the OnControllerColliderHit function you want to send a message to the object you've collided with that contains the name of the function you want to call and the ID of the object you hit, just to be safe.

 /* In the script attached to the character */
 function OnControllerColliderHit (hit : ControllerColliderHit) { 
      hit.gameObject.SendMessage("RecieveMessage", hit.gameObject.GetInstanceID());
 }

 /* In the script attached to the object */
 function Recievemessage (id) {
      if(id == gameObject.GetInstanceID()) {
           print id;
      }
 }

So essentially the CharacterController hits the object then tells the object "hey, I hit you." Seems to work in my game. Hope this helps.

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 ChronMcDon · Jul 17, 2013 at 07:13 PM 0
Share

This is the right way to do it.

Object $$anonymous$$essages are the shit.

avatar image dbrizov · Jan 05, 2016 at 07:22 PM 0
Share

A good solution indeed, but not entirely correct. OnControllerColliderHit() is called only when the character is moving. If the character is standing still, and something hits him, OnControllerColliderHit() won't be called.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Detection wether char. controller hits char. controller 0 Answers

Make Character Controller a Box? 2 Answers

My Character Controller Is Warping Randomly (/w video) 0 Answers

Collision and character controllers 0 Answers

No Collision with character controller 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