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 potatongravy1 · Jul 08, 2014 at 09:34 AM · physicsraycastinput

Raycast Ignoring Distance

Hi There.

I have setup a raycast to fire when I hit the mouse button, but it is completely ignoring the distance parameter and I don't know how to fix it, any help would be great!

Here is my code:

 var fwd = transform.TransformDirection(Vector3.forward);
     
 var hit : RaycastHit;
     
 if(Input.GetKeyDown(KeyCode.Mouse1))
 {
 if(Physics.Raycast(transform.position, fwd, hit, 1))
 {
     print("I Hit " + hit.transform.name);
 }
 

Cheers, Bradley.

Comment
Add comment · Show 7
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 Dudledok · Jul 08, 2014 at 10:03 AM 0
Share

Have you tried replacing hit with a float to see what happens? If you put, say, 10 there ins$$anonymous$$d and it works, then your issue is with your hit variable.

avatar image potatongravy1 · Jul 08, 2014 at 10:15 AM 0
Share

It is going above the distance.

avatar image potatongravy1 · Jul 08, 2014 at 10:15 AM 0
Share

I will try the variable idea now!

avatar image potatongravy1 · Jul 08, 2014 at 10:17 AM 0
Share

The variable idea did not work.

avatar image HarshadK · Jul 08, 2014 at 10:26 AM 0
Share

Check

 Debug.Log(hit.distance);

for the object which it hits so that it becomes clear if it is actually exceeding the limit of the distance provided in argument.

Show more comments

1 Reply

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

Answer by Owen-Reynolds · Jul 08, 2014 at 02:43 PM

What kind of testing set-up do you have? I find, that when something seems broken enough I have to stop and ask, it's worth making a real test scene.

Paste just your code into an empty, in a new scene. Make just one cube to fire at. Change the code a little to make testing easier -- raycast every frame and always print something. I use C#, so this is probably a bit wrong:

 var result : string;

 function Update() {
   if(Physics.Raycast( transform.position, transform.forward, hit, 1)) {
     result = "hit " + hit.transform.name + " at + " hit.distance";
   }
   else {
     result = "missed";
   }
 }

Move the empty around in scene view, to test. result will always right-away tell you about the raycast (as long as the empty is selected.) I think distance will work fine. But then you can add more parts of your real scene, and find the broken part (do you have any large triggers?)

NOTES: transform.forward is the short version of transformDir(V.forward). Both versions just give your local blue arrow, and transform.forward is shorter. Double-check the parms. There are so many versions of raycast that it's easy to switch distance and layermask, but what you have looks good (and it exactly matches an example in the docs. But, in general, easy to flip inputs.)

I wouldn't bother drawing a ray. The syntax is a little different than for a raycast, so it's easy to have the ray be wrong, or have it confuse you about the raycast. Plus, the ray is your local blue arrow. DrawRay is great for more complex stuff. Like your enemies are avoiding imaginary obstacles and you need to know what all the "feeler" raycasts think is blocking them.

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 potatongravy1 · Jul 09, 2014 at 01:14 AM 0
Share

Thanks for your help. I will give this all a go very soon.

Cheers.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Mouse Input for RayCast - Update or FixedUpdate 1 Answer

Raycasting a specific square area 1 Answer

Stop raycast from hitting same gameObject twice 2 Answers

Plane.Raycast layerMask? Using both Physics and Plane raycast 0 Answers

How to raycast from mouse position in scene view? 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