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 Ashsaber · Nov 12, 2014 at 05:00 AM · c#collisionraycast

Raycast from camera not hitting Terrain

So, I have this.

 private void getInputData()
 {
         //gets the mouse click position and places a unit at that location.
         RaycastHit hit;
         Ray ray = playerCam.ScreenPointToRay(Input.mousePosition);
         if(Physics.Raycast (ray ,out hit, 1000))
         {
             Debug.Log(hit.point);
             if (hit.collider.gameObject.tag == "Terrain")
             {
                 //Creates a unit if nothing is there.
                 Debug.Log("I hit the terrain!");
                 createSelectedUnit (selectedUnit, hit.point);
             }
             else if (hit.collider.gameObject.tag == "Melee" ||
                      hit.collider.gameObject.tag == "PlayerHero")
             {
                 //This is a blanket if/then statement for all units that the player can control.
                 //If player hits the unit, show some info on GUI.
             }
             else if (hit.collider.gameObject.tag == "Dist_Melee" ||
                      hit.collider.gameObject.tag == "EnemyHero")
             {
                 //This is a blanket if/then statement for all units that the enemy controls.
                 //If player hits the unit, show some info on GUI.
             }
         }
 
 }

So I'm shooting a ray from a camera at a terrain at (value, 0, value) and according to the hit.point debug it's not hitting the ground half the time. That is, it's not (value, 0, value) being shown but (value, value, value).

My question is this: What do I do to ensure that the ray being shot from the camera will always hit the ground?

Bonus points if you can shed some light on why the ray from the camera isn't hitting the ground to begin with.

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 Ashsaber · Nov 12, 2014 at 02:18 PM 0
Share

Problem solved. Cause was between chair and keyboard. I added a collision node that was overly large and it was messing with the raycast.

1 Reply

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

Answer by robertbu · Nov 12, 2014 at 05:04 AM

First, debug it. On line 8, put:

 Debug.Log(hit.collider.name + ", " + hit.collider.tag);

This will tell you what (if) you are hitting. Second, if you only want to detect the terrain, then use Collider.Raycast() rather than Physics.Raycast():

  if ( Terrain.activeTerrain.collider.Raycast(ray, out hit, 1000f) ) 
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 Ashsaber · Nov 12, 2014 at 02:17 PM 0
Share

So apparently I made a huge collision node on the player cylinder and totally forgot about it. ._. Deleting it solved every problem in the history of mankind. Woo!

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

2 People are following this question.

avatar image avatar image

Related Questions

Is it possible to check how much a trigger is filled by %? 1 Answer

c# How do I set up continuous raycast collision detection? 1 Answer

Raycast Mouse Click On Specific Objects Only 0 Answers

Creating 2d image map of objects based off 3d space colliders Unity C# 0 Answers

Multiple Cars not working 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