Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Kvart · Dec 01, 2015 at 10:24 AM · raycasting

Detecting if mouseclick is within path

Hey good people, i have a question for you which i hope that i'm able to explain.

I'm making a tower defence game in 3d. In the game you put out a rallyflag to place some soldiers. I'm detecting the mouseclicks by raycasting on a plane collider, which is working fine. One thing a cannot figure out though is how to check if that mouseclick is within the curvy path. My first thougth was to put colliders on the path, either roughly with cubes and box colliders or by making a 3d volume of the path in an external 3d program and then making a mesh collider for that volume. The problem with the first idea is that i don't know how to raycast on multiple combined colliders, is this even possible? And i'm afraid that the other idea would take to much performance power, it seems a bit overkill to make a mesh collider for this problem.

Check out the code below: The objectsurface is the plane collider i'm raycasting on. You can also see the rallyDistance which is the maximum distance from the tower which the soldiers can be placed.

 RaycastHit hit; 
 Collider coll = objectSurface.GetComponent<Collider>();
 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 

 if(coll.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),out hit,100.0f)){
    Vector3 mouseP = hit.point;
    if(Vector3.Distance(mouseP, gameObject.transform.position) <= rallyDistance){
       objectRallypoint.transform.position = mouseP;    
    } else{
       Debug.Log("out of range");
    }
 }

As i mentioned this code works fine expept it doesn't check if the click is within a path. It seems like such a simple thing, but i can't get my head around it. I hope any of you have some good ideas of how to get around this. Thanks 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

1 Reply

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

Answer by kevinspawner · Dec 01, 2015 at 10:32 AM

You need to add a lowpoly mesh and use that as a collider, Since you have a long path, you dont need to break the mesh in to different objects. Just use the external 3D application to build a lowpoly around the path and use that as a mesh collider. I dont think, it will be very expensive in memory, of course it will be quiet higher compared to box collider, but it should be alright in your case.

Comment
Add comment · Show 1 · 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 Kvart · Dec 01, 2015 at 04:31 PM 0
Share

Also the best solution i could think about. Only problem with this solution is that the path has to be made perfectly in the external 3d program together with the mesh collider. But other than that it's a good solution, tried it out and it delivers that right result.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Click Point From Raycasting Not Yielding Expected Coordinates 1 Answer

SInce when?!?!? (Raycast issue) 2 Answers

Checking the position of the mouse cursor in 3d space 2 Answers

How to detect certain object using raycast 2 Answers

Having problems changing the material of current target. 2 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