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 Kona · Oct 08, 2010 at 09:06 AM · javascriptcollisioncolliderscriptingbasics

How to make collision check if other collider has component

Hi! I've quite recently started learning javascript and I still have quite some trouble with it. I'm currently working on a RPG-game and have started a test scene, it contains:

EnemyCube <- Atm static cube, with a box collider and a script that senses if mouse is over it and makes it change colors aswell as mouse cursor-texture. If mousebutton is pressed when it's hovering over it the variable "selected" is changed from 0 to 1.

PlayerCube <- Moves to where the mouse is clicking, it has a box collider aswell as an empty child-object with a sphere collider ("RangeCollider"), I want the rangecollider to kick in when it senses another Collision, I want it to check if the other colliders object is tagged with the tag "monster"

In case the tag is "monster" I want it to read if the "selected" variable is 1 in the EnemyCubes script, in that case I also want the PlayerCube to call "function isInCombat()"

My problem is the part where I want my "RangeCollider" to look if the other collider has the EnemyScript and if that scripts "selected" variable is set to 1. I'm kinda clueless.

Comment
Add comment · Show 1
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 · Oct 08, 2010 at 09:15 AM 0
Share

Your question is well-presented and the concepts clearly explained, but it always helps to include script examples so we have something to work with!

1 Reply

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

Answer by · Oct 08, 2010 at 09:12 AM

This is just a quick-and-dirty untested approach, but hopefully it explains the concepts.

function OnCollisionEnter ( hit : Collision ) { if ( hit.gameObject.tag == "monster" ) // did we collide with a gameObject tagged 'monster' { var monster = hit.gameObject.GetComponent(EnemyCube); // store the EnemyScript to a local variable

 if ( monster )                        // if we found an EnemyScript on the monster
 {
    if ( monster.selected == 1 )       // if the monster's selected value is 1
    {
       // Do your thing
    }
 }

} }

By the sounds of things, all you were missing was the GetComponent call to find the EnemyScript. If you're unable to work this into your code, post what you have and I can adapt it to your approach.

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 Kona · Oct 08, 2010 at 05:47 PM 0
Share

Thanks alot! :D It worked very well!

avatar image Kona · Oct 08, 2010 at 05:51 PM 0
Share

*Err. sorry for double-commenting but in case another beginner has the same problem in the future keep this in $$anonymous$$d if you haven't thought about it already:

I had to move my mouse cursor to the ignore_ray layer (could probably work with some coding with physics_ignore aswell I guess?)

If I didn't do that the mouse couldn't reach the enemy-Object 'cause the "RangeCollider"-collider I had on the player was blocking it. I'm not sure how, I guess the mouse should ignore the enemy collider aswell? but meh, it worked for me :D

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

No one has followed this question yet.

Related Questions

Trigger Enter and Exit not working properly? 2 Answers

Generating A Custom Collider For Multiple Meshes - Script 2 Answers

Destroy On Collision will not work with spawned AI 1 Answer

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers


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