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 mmangual83 · Apr 10, 2012 at 03:39 PM · 2dcollision3dspherebox

2D on 3D collision

So my team and I are making a first-person 3D game on Unity (who knew :p), and we are having a bit of an issue. You see, we are trying to make our character's cross hair collide with enemies and other objects within the game. The cross hair is done as a 2D texture. The crosshair turns red if its in range of an enemy, green if it is an interactive object (buttons, health packs, etc). We have tried various ways, even adding a 3d box collider to the character that if he detects colision then the color of the crosshair changes (didn't work), we also tried getting distance formula (didn't work). Our last chance is to attempt a 2D on 3D collision but we just don't know how to even get started. Can someone out there show us a way to solve this?

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
1

Answer by Shawn Miller · Apr 10, 2012 at 04:17 PM

You actually want to use Raycasting for this problem. Below is an example of how you could implement this though depending on how you identify an interactive object separate from an enemy or a regular object will change the conditions.

Keep in mind, I did this with the assumption that the crosshair is a child object attached to your player since that's what you implied. If that's the case, then this script would have to be attached to the crosshair object. If it's a GUI element, then you would handle it slightly different but the form remains the same.

     RaycastHit hit;
     Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit);

     if(hit) {
         if(hit.transform.tag == "Enemy" && hit.distance <= 50) {
             renderer.material.color = Color.red;
         } else if (hit.transform.tag == "Interactive" && hit.distance <= 20) {
             renderer.material.color = Color.green;
         } else {
             renderer.material.color = Color.white;
         }
     }


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 mmangual83 · Apr 10, 2012 at 08:36 PM 0
Share

Thank u shawn miller, but I am getting an error saying that BCE0044: expecting ), found 'hit'. Then it says, BCE0043 : Unexpected token: ).

avatar image Shawn Miller · Apr 11, 2012 at 01:13 AM 0
Share

That means there's a syntax error somewhere, which line is it on?

avatar image Bunny83 · Apr 11, 2012 at 01:22 AM 1
Share

That's because this example is written in C# and you copied it into a UnityScript (Javascript) file. If it's in a C# file you have to put it into a class. This isn't a whole script, it's just a script fragment.

ps:

 transform.TransformDirection(Vector3.forward)

is the same as

 transform.forward
avatar image
0

Answer by roamcel · Apr 10, 2012 at 04:18 PM

The way you normally do this is by using screenpointtoray

Basically you convert a screen position (from the mouse cursor) into a ray that you use to verify collisions, and act consequently in the scene.

Otherwise, you can do the 'inverse' and trigger a "mouse cursor" (a plane gameobject always following the camera in a billboard, with a texture) change through the

 OnMouseOver

function. Note that you need a collider and usually a rigidbody for onmouseover to work.

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.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycast for 2d with touch. 0 Answers

Isometric projectile collision 2D or 3D? 0 Answers

Sphere with constant speed and bouncing attribute. 1 Answer

3d Collision for 2d objects 1 Answer

How do I set a sprite to a sphere? 0 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