• 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
Question by LPGaming-Company · Jan 23, 2013 at 05:21 PM · bugraycastingglitch

Inaccurate Raycasting

NOTE:: This only occurs AFTER moving the camera.... So I'm guessing it has something to do with SmoothFollow

I've uploaded a web-player version so you can see what's going on, please note everything in here is basic and the GUI will be mal-scaled because I haven't worked on scaling anything yet. Either way, here it is in a nutshell....

The raycasting seems to come from the camera, although everytime I use it, it starts at a location lower then the last, which in term is making the raycast very inaccurate. The camera has a smooth-follow script attached to the player, if this could be causing the problem?

  • Hit Y and Z to spawn a new NPC (They have different names, only visible in Console currently)

  • Will spawn a capsule, just look for it, each NPC will be +10z away from eachother

  • Click to target, healthbar will appear to right if targetting

Note: You CAN target it, you just have to click all around it numerous friken times... Http://LP-Gaming.com/WebThing.html

Raycasting code used

  if (Input.GetMouseButtonUp(0))
         {
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast (ray, out hit, 250))
             {
                 if(hit.transform.tag == "Untagged") return;
                 if (hit.transform.tag != "terrain")
                 {
                     target = hit.transform;
                     Debug.Log ("Hit something: " +hit.transform.tag);
                 }
                 } else {
                     target = null;
                 }
             }


EDIT: While debugging further I've noticed that even while clicking on the NPC, 70% of the time the Raycast hits the terrain, not the NPC... I'm not sure what would be causing this, is it going through the NPC?

Edit2: using Debug.DrawLine I noticed that there were two ray-cast lines being drawn, one hitting where it should, and another coming from somewhere unknown (Not the camera, although below it) hitting slightly before it should, scaled down at a lower angle... So, I believe I found my brother, now I just need to find a fix.

EDIT3: It seems that if I HOLD the raycast button down for around 20seconds the secondary Raycast will gradually fall below the terrain and not interact with anything anymore, although this still worries me as to why there is a secondary Raycast.

Comment

People who like this

0 Show 9
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 Dave-Carlile · Jan 23, 2013 at 05:27 PM 0
Share

Add more Debug.Log calls. Does the raycast hit anything at all? Log the tag immediately inside the if.

avatar image $$anonymous$$ · Jan 23, 2013 at 05:56 PM 0
Share

It's hard to believe it's "stupidly inaccurate", because it's made to work, and it usually just works.. what if you put a debug.log outside the raycasting, to see if the mouse up actually happens there. Also what about the rest of Update? Maybe you return from the method prematurely somewhere.

avatar image LPGaming-Company · Jan 23, 2013 at 05:56 PM 0
Share

While doing this I noticed that even while clicking on the NPC half the time, about 80% of the time it sent me this message

"Raycast hit something with tag: Terrain"

and then the other 20%

"Raycast hit somethign with tag: Mob" <-- NPC

So, the raycast is going through the Mob i guess?

avatar image AlucardJay · Jan 23, 2013 at 06:04 PM 3
Share

Debug.DrawLine or LineRenderer is also a good way of visually displaying a ray cast to see what is actually happening. Just use the camera position and the hit point.

avatar image LPGaming-Company · Jan 23, 2013 at 06:23 PM 0
Share

Alright, this is some strange stuff.. Using the Debug.DrawLine I found two lines being drawn, one I couldn't figure out where it was coming from, and the other from the camera.. One of the was hitting slightly before the other...

EDIT: It's coming from under the camera, almost at terrain level.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by Bunny83 · Jan 24, 2013 at 12:13 AM

It feels like you're doing this in FixedUpdate. Could that be? I'm too lazy to download and decompile :D

Never do such things in FixedUpdate. FixedUpdate is for physics calculations. You can't process any event information there because events are evaluated once per frame. If your visual framerate is greater than the fixedUpdate rate it's pure luck when a MouseDown event happens in exact the same frame where a FixedUpdate is called.

I've made a visualisation of how FixedUpdate works some time ago. Just play with the "framerate" setting and see when Update is called and when FixedUpdate is called.

Comment
$$anonymous$$

People who like this

1 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 LPGaming-Company · Jan 24, 2013 at 09:53 AM 0
Share

Thanks, but this is not the case as you can see below

 public void Update () {
         //Debug.Log (attackTimer);
         if(attackTimer > 0) {
             attackTimer -= Time.deltaTime;
         }
         if(Input.GetKeyUp (KeyCode.O)) {
             i++;
             Debug.Log ("Pressed O : " + i + " Times");
         }
         if(Input.GetKeyUp (KeyCode.K)) {
             if(target != null) {
                 if(target.tag == "Mob") {
                     NPCs.GetInstance().npcList[target.gameObject].currentHealth -= 10;
                     Debug.Log ("Target's current Health = " +NPCs.GetInstance().npcList[target.gameObject].currentHealth);
                 }
             }
         }
         if(Input.GetKeyUp(KeyCode.Y)) {
             NPCs.GetInstance().createNPC("Chris", 100, 50, 10, new Vector3(130.0f,2.5f,Z));
             Z+= 4.0f;
         }
         if(Input.GetKeyUp(KeyCode.Z)) {
             NPCs.GetInstance().createNPC("Kyle", 100, 50, 10, new Vector3(130.0f,2.5f,Z));
             Z+= 4.0f;
         }
         if (Input.GetMouseButtonUp(0))
         {
             i++;
             Debug.Log ("Clicked " + i + " Times");
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast (ray, out hit, 100))
             {
                 Debug.Log ("Hit something: " +hit.transform.tag);
                 Debug.DrawLine(Camera.main.transform.position, hit.point);
                 if(hit.transform.tag == "Untagged") return;
                 if (hit.transform.tag != "terrain")
                 {
                     target = hit.transform;
                     
                     Debug.Log ("Hit something: " +hit.transform.tag);
                 }
                 } else {
                     target = null;
                 }
             }
avatar image Bunny83 · Jan 24, 2013 at 09:57 AM 0
Share

Do you get the "Clicked ..." message reliably? Because when i tested it yesterday deseleting was unreliable as well.

Are you sure you have only one camera tagged MainCamers?

avatar image LPGaming-Company · Jan 24, 2013 at 10:33 AM 0
Share

Yes, the project is very basic. There's a terrain, a player, and a camera... That's all.

and yes, I was clicking the Clicked message every time.

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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

16 People are following this question.

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

Related Questions

Unity Gllitching Everything? 1 Answer

A node in a childnode? 1 Answer

Unity completely glitching out 0 Answers

Differentiating between seperate game objects in code when one script is attached to multiple game objects? (2D) 3 Answers

Flickering issue when objects touch terrain. 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