Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Aug 26, 2011 at 06:59 PM by Pe-ads for the following reason:

The question is answered, right answer was accepted.

avatar image
0
Question by Pe-ads · Aug 24, 2011 at 09:16 PM · raycastfallingdrawrayrays

Rays drawn using Debug.DrawRay "fall" in editor view

I'm casting five rays from my camera to determine where camera is looking. I'm also using Debug.DrawRay to make them visible in editor mode. However, when I run the game and switch to editor something odd starts happening.

There is one set of rays where the rays should be, but they are flashing. Another set of (solid, non-flashing) rays fall down from where the other rays are. I have no idea why this is happening. Here's my code:

 var top : Vector3;
 var right : Vector3;
 var bottom : Vector3;
 var left : Vector3;
 
 var castDirection : Vector3;
 castDirection.z = 1;
     
 var topx : Vector3;
 var rightx : Vector3;
 var bottomx : Vector3;
 var leftx : Vector3;
 
 var topxs : String = topx.ToString();
 var rightxs : String = rightx.ToString();
 var bottomxs : String = bottomx.ToString();
 var leftxs : String = leftx.ToString();
 
    
 private var hit1 : RaycastHit;
 private var hit2 : RaycastHit;
 private var hit3 : RaycastHit;  
 private var hit4 : RaycastHit;
 private var hit5 : RaycastHit;
 
 private var hit1Bool : boolean;
 private var hit2Bool : boolean;
 private var hit3Bool : boolean;
 private var hit4Bool : boolean;
 private var hit5Bool : boolean;
     
 function Update () {
 
 topx = transform.position + top;
 rightx = transform.position + right;  
 bottomx = transform.position + bottom;  
 leftx = transform.position + left;
   
        //Rays from crosshair
        //Center ray/hit1
        if(Physics.Raycast(transform.position, castDirection, hit1, 2)){
 
 
        }
 
        //Top ray/hit2
        if(Physics.Raycast(topx, castDirection, hit2, 2)){
 
        }
 
        //Left ray/hit3
        if(Physics.Raycast(leftx, castDirection, hit3, 2)){              
 
 
        }

        //Bottom ray/hit4
        if(Physics.Raycast(bottomx, castDirection, hit4, 2)){
            
 
        }
 
        //Right ray/hit5
        if(Physics.Raycast(rightx, castDirection, hit5, 2)){              
 
        }          
 
        //Editor/debug rays
          //Center debug ray
        Debug.DrawRay(transform.position, castDirection * 2, Color.green);
 
        
        //Top debug ray
        Debug.DrawRay(topx, castDirection * 2, Color.green);
 
        
        //Right debug ray
        Debug.DrawRay(rightx, castDirection * 2, Color.green);
 
   
        //Bottom debug ray
        Debug.DrawRay(bottomx, castDirection * 2, Color.green);
 
        
        //Left debug ray
        Debug.DrawRay(leftx, castDirection * 2, Color.green);
  }


Edit: The variables top, right, bottom and left are set in the Inspector to (0,1,0), (1,0,0), (0,-1,0) and (-1,0,0) respectively.

I'm using the latest version of Unity, and my scene isn't processor-intensive in the slightest (primitives with no textures and just one light).

Can anyone identify what's happening here?

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 aldonaletto · Aug 24, 2011 at 09:29 PM 2
Share

Do you initialize top, bottom, right and left at the Inspector? Because with this code they all will be equal to Vector3.zero, what would let topx, rightx, bottomx and leftx = transform.position.

avatar image Pe-ads · Aug 25, 2011 at 07:32 AM 0
Share

Apologies, they are set in the Inspector. I've updated the question accordingly.

1 Reply

  • Sort: 
avatar image
3
Best Answer

Answer by aldonaletto · Aug 25, 2011 at 11:23 PM

I tested this script, and nothing strange happened. From the camera position, 5 green rays go in the forward direction, as it should be. They don't flash, nor are other rays appearing in the scene. Nothing in this script can do this, so I suspect something else must be the culprit - probably another instance of this same script forgotten in some object, and with castDirection.y = 1. If you can't find any lost instance of this script, I suggest you to create a new empty scene and test the script there.

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 Pe-ads · Aug 26, 2011 at 09:46 AM 0
Share

Thanks a lot! I wonder why I didn't try it in a new scene in the first place >.> I'll try that out later today and get back to you :D

avatar image Pe-ads · Aug 26, 2011 at 06:58 PM 0
Share

Well, I tried it in a new scene and it was fine. So I tried it in my main scene without any other assets except for the light and the floor, and it didn't work. in the end I just delete and re-added the First Person Controller and it was fine. No idea why it was messing up.

Thanks a lot for your help! Really highlighted that I should try re-importing/deleting and replacing assets after checking for code errors!

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can I draw a specific distance with Debug.Drawray 4 Answers

Finding RayCastHit's Origin Position 2 Answers

What does direction do for a physics.raycast besides direction? (read details) 1 Answer

How to place an object in the direction of a raycast 1 Answer

What is the best way to send raycasts out in a sphere? And what resolution do I need to be accurate? 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