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 thecobe · Jul 23, 2014 at 02:20 PM · raycast

Why Isn't My Raycast Working?

I have a script that casts a ray down the center of the screen and it finds the distance of objects and other stuff, it worked fine yesterday, but today it isn't finding distance and I assume it's because it's not even being cast in the first place. If anyone knows what is wrong I would really appreciate help.

 #pragma strict
 
 var Distance : int;
 var Holding = false;
 
 private var lineTransform : Vector3;
 private var startTransform : Vector3;
 
 function Update ()
 {
     if (Input.GetButtonDown("Fire1"))
     {
             Path();
     }
 }
 
 function Path()
 {
 
 var hit : RaycastHit;
 var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
 
 {
     if(Holding == false)
     {
         if (Physics.Raycast (ray, hit, 100))
         {
             Distance = hit.distance;
             
             if(hit.transform.tag == "PickUp")
             {
                 hit.transform.parent = transform;
                 hit.rigidbody.useGravity = false;
                 Holding = true;
             
                 lineTransform = hit.point;
         }
     }
     if(Holding == true)
     {
         if (Physics.Raycast (ray, hit, 100))
         {
             Distance = hit.distance;
             
             if(hit.transform.tag == "PickUp")
             {
                 hit.transform.parent = null;
                 hit.rigidbody.useGravity = true;
                 Holding = false;
             
                 lineTransform = hit.point;
             }
         }
     }
 }
 }
 }

Comment
Add comment · Show 2
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 Graham-Dunnett ♦♦ · Jul 23, 2014 at 02:21 PM 1
Share

Please learn how to format code. I did this for you on this occasion. Questions get rejected normally if the poster hasn't bothered to write a good question.

avatar image Graham-Dunnett ♦♦ · Jul 23, 2014 at 02:23 PM 0
Share

Just add some Debug.Log() messages so you can see where the code gets to. Or debug using $$anonymous$$onoDevelop.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by glad · Jul 23, 2014 at 02:34 PM

It could be that your camera is too far from the hittable area. Try to increase a ray distance in "Physics.Raycast (ray, hit, 100)" from 100 to 1000 for example.

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 thecobe · Jul 23, 2014 at 03:27 PM 0
Share

Thanks for the response, but it still isn't working.

avatar image
1

Answer by deathripper · Jul 23, 2014 at 03:33 PM

You could also use "Debug.DrawRay" which is nice for Debugging since it Shows you your actual raycast in what ever Color you like.

http://docs.unity3d.com/ScriptReference/Debug.DrawRay.html

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
avatar image
0

Answer by thecobe · Jul 23, 2014 at 04:03 PM

I actually got it to work by just restarting and using the same script, so I don't really know what was wrong, but now I can't get it to set the objects parent to null, if anyone knows what's wrong please tell me.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

throw an object by swiping on it 0 Answers

3rd Person camera collision 2 Answers

How To Get Current Animation Name 3 Answers

Not firing a ray (C#) 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