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 Coreyf716 · Nov 25, 2012 at 07:54 PM · collisionraycastcollidercharactercontrollerraycasthit

Detect RaycastHit on Character Controller

As you may know, colliders can't be used with character controllers. I have a script using raycast that is supposed to detect a hit on the player, which has a character controller component attached. How can I get the player to detect a raycast hit?

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
0
Best Answer

Answer by Griffo · Nov 25, 2012 at 08:44 PM

 if(hit.collider.gameObject.tag == "Player"){
     // Do Stuff
     }
Comment
Add comment · Show 8 · 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 Coreyf716 · Nov 25, 2012 at 09:46 PM 0
Share

It doesn't help anymore than my code:

if (hit.collider.name == "Player") { // Code }

avatar image Griffo · Nov 25, 2012 at 09:50 PM 0
Share

Works fine for me ..

Post your code.

avatar image Owen-Reynolds · Nov 25, 2012 at 10:00 PM 0
Share

Char controllers are really odd beasts. Their built-in capsules count as rigidbody colliders for most purposes. Sort of an undocumented(?) feature.

avatar image Bunny83 · Nov 25, 2012 at 11:51 PM 1
Share

@owen-reynolds:

It's not really an undocumented feature ;) As you can see on the CharacterController page the CharacterController is derived from Collider, so it's just a normal collider.

$$anonymous$$eep in $$anonymous$$d that the CC movement (the $$anonymous$$ove and Simple$$anonymous$$ove functions) bypass the physics and perform their own physics when moved. Besides that it's just a capsule collider (which can only be rotated around the y-axis btw).

@coreyf716:

It doesn't make much sense to use GameObject.Find when you already have a reference to your player. Especially when it might be a multiplayer game there might be multiple objects named player.

Just do it like this:

 if (Physics.Raycast(transform.position, forward, hit, range))
 {
     if (hit.collider.name == "Player")
         hit.collider.GetComponent(Health).decreaseHealth(25);
 }
avatar image Bunny83 · Nov 27, 2012 at 01:15 AM 1
Share

Do you get any errors in the console?

If so it's most likely that the object you've hit, that is named "Player" doesn't have a Health script attached.

If you don't get an error you doing something wrong.

I've just took a look at your prefab package. It's a bit messed up on my end. Your scripts are not included and the child objects have no name. Furthermore you still have an additional capsule collider as child which might intercept your raycast. Remove this collider. Again, the CharacterController IS already a collider.

Show more comments
avatar image
2

Answer by Owen-Reynolds · Nov 26, 2012 at 02:49 PM

There are a lot of things that can go wrong with ray casts: bad direction, too short, hitting a trigger box by mistake... so best to do lots of checking. Try to check each step. This would check where it ends and what you hit:

 var rayEnd : Transform // drag in a small red cube w/no collider
 var hitWho : string; // debugging

   // Update:
   // so we can see where ray goes:
   rayEnd.position = transform.position + forward*range;

   if(Physics.Raycast( ... ) {
     hitWho="hit:"+hit.transform.name;
     ...
   }
   else hitWho="<no hit>";

Some people also recommend using `Debug.DrawRay` to see the path, but the inputs aren't the same as raycast, so it's easy to draw it in the wrong place and confuse yourself even more.

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 Coreyf716 · Nov 26, 2012 at 10:01 PM 0
Share

I don't understand the purpose of doing that. It doesn't seem to work.

avatar image Bunny83 · Nov 27, 2012 at 01:27 AM 0
Share

@Coreyf716: You don't understand the purpose of debugging? A computer never does something the wrong way. It always just do what you or another programmer told it to do. It seems you don't get the result you would have expected, so you have a bug in your game / program.

It's very unlikely that there is a bug in Unity itself, it's possible but you shouldn't start with that assumption.

Since it's not doing what you want it's your task to figure out what it is actually doing, either to understand how it works and / or to spot the problem. By storing the name of the object your ray hits in a public variable you can see in the inspector what your raycast has hit.

avatar image Coreyf716 · Nov 27, 2012 at 01:55 AM 0
Share

Yeah, I understand it. It's the way that my code and his worked together.

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

Raycast Not Drawing In Target Direction 0 Answers

How do I check for collisions when I move objects based on Time.deltaTime? 3 Answers

Physics.Raycast not hitting anything 1 Answer

Getting Character controller to fit model? 0 Answers

When using a character controller for the player, can other colliders be used alongside the 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