• 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
1
Question by Grim420-2 · Dec 14, 2009 at 12:59 AM · collision

Check for a CharacterController collision from a non-CharacterController object

So I've been learning about collision detection and I think I get it for the most part.

I can use a script with OnCharacterControllerHit on my CharacterController to detect if it hit anything, and I can use a script with OnCollisionEnter on an object to see if another object hits it. This is all fine, but I seemed to have hit a snag when I try to put a script on an object to see if a CharacterController hits it.

Does this make sense? I'm trying to write a script for all the switches in my level. One script that I put on each switch and it checks for what switch its attached to and if the player touches it. It seems that I have to check for the collision with a script on the CharacterController and then send some kind of signal to the switch script.

Is there a shorter way to do that? I'm trying to keep the scripts as simple as possible. Maybe something like using my switch script to GetComponent.CharacterController.collider.hit from the CharacterController script and checking the objects that the controller hit... or something along those lines.

How do I check for a CharacterController collision from a different object?

Thanks.

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

Answer by Jaap Kreijkamp · Dec 14, 2009 at 06:06 AM

I think it would be easier to give your player a good tag like 'Player', make the switches trigger Colliders (just check isTrigger in collider properties), and in the code of your switches have something like:

function OnTriggerEnter(col : Collider) {
    if (col.gameObject.tag == "Player") {
        // do fancy stuff
    }
}
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
2

Answer by andeeee · Jan 11, 2010 at 09:19 PM

You can check the type of the incoming collider using the GetType method:-

if (collider.GetType() == CharacterController) {
    ...
}
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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

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

Ignore collision on tag not working 2 Answers

Movement around a huge object by avoiding obstacles 1 Answer

BoxCast, OverlapArea, and Raycasting; ground detection questions 0 Answers

[Solved] Is it okay if we change transform.position on a kinematic rigid body which also does not use Gravity? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges