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 windexglow · Sep 23, 2010 at 08:21 PM · raycastrayexplain

Confused as to how my RaycastHit script works

var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    var hit : RaycastHit;
     if (Physics.Raycast (ray, hit, 1000)) {
           // Debug.DrawLine (ray.origin, hit.point);
            mouse_icon.transform.position = hit.point;////ray.GetPoint(hit.point);
           // Debug.Log(hit.point);
    }

That works correctly. There are no problems with it that I see.
It puts mouse_icon where my mouse is. It also puts it in the game space in the right spot.

But I'm confused how that is doing it.
If I remove that "if" in it, it no longer works. Yet I didn't set a variable in it.

To expand : So doing that 'if' statement controls where 'hit' goes? If I do another 'if' statement, would it reset 'hit' to the new info?

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
1
Best Answer

Answer by skovacs1 · Sep 23, 2010 at 08:33 PM

You really have to be more specific than "it no longer works" - that doesn't tell me what part of it you expected to work and how. When you say you removed the if statement, I don't know if you mean that you simply removed the if or that you completely removed the raycast as well.

See the docs on Physics.Raycast;

You did set a variable: hit. The hitInfo parameter is specified with the out keyword. The variable is being set by the raycast. If you remove the raycast completely, hit will be null;

Physics.Raycast returns true if it hit something (see the docs). If you don't check whether it hit something, when you don't hit anything, what do you thing hit is going to contain? hit will likely be null or contain default values.

Since you're using hit, when it has an invalid value, what do you think will happen when you assign mouse_icon.transform.position to hit.point? If hit is null, then it shouldn't do anything and if it contains some default values, then it won't do anything meaningful.

Comment
Add comment · Show 2 · 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 · Sep 24, 2010 at 12:41 AM 0
Share

$$anonymous$$inor point: you don't need the 'out' keyword in UnityScript.

avatar image skovacs1 · Sep 24, 2010 at 01:55 PM 0
Share

You don't really, but the docs have it, specifying the parameter as a return by reference.

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

No one has followed this question yet.

Related Questions

LineRenderer (Laser Beam) is not following the ray it's going on the wrong direction when reflecting 1 Answer

Trouble with Ray & Raycasting and AddForce() 1 Answer

Raycasting and foreach loops 1 Answer

how do i fix this small error 2 Answers

How can I apply damage based on a grenade explosion, but test whether the objects are behind cover so they are not damaged even within the explosion radius? 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