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 Fascination St · Dec 13, 2012 at 09:27 AM · raycastnoobsimple

Possibly simple Raycasting question

Hey guys, so I have a raycast script attached to my FPS controller, with the raycast coming out of my main camera/fps camera.

What the script is meant to do is when the player approaches the tagged door within the range, then lets you have the option to press space to go to the next area. This all works fine. However when I go toward the door, looking at a weird angle (but still with the ray on the door) it doesn't detect it, or it still thinks it is being detected even when the player is outside the range. Here's the script if anyone could help :)

 var Hit: RaycastHit;
 var Range : float;
 var mesh :GameObject;
 var DoorActive: boolean = false;
 private var count: int = 0;
 var myStyle1 : GUIStyle;
 var Level : String;
 
 
 
 function OnGUI () {
     if(DoorActive == true){
     GUI.color = new Color( 1,1,1, 0.5f); 
     GUI.Label (Rect (Screen.width/2-50, Screen.height/2-25, 100, 50), "Space to Enter",  myStyle1);
     }else{
     GUI.Label (Rect (Screen.width/2-50, Screen.height/2-25, 100, 50), "o",  myStyle1);
     }
 }
 
 
 function Update(){
 
  if(DoorActive == true){
  if(Input.GetKey(KeyCode.Space)){
  count = count + 1;
  Debug.Log("Door presses" + count);
  Application.LoadLevel (Level); 
  DoorActive = false;
  //what happens when press
  }
  }
  var DirectionOfLine : Vector3 = mesh.transform.TransformDirection(Vector3.forward);
  Debug.DrawRay(mesh.transform.position, DirectionOfLine * Range, Color.yellow);
  if(Physics.Raycast(mesh.transform.position, DirectionOfLine, Hit, Range)){
  if(Hit.collider.gameObject.tag == "Door"){
  
 // Debug.Log("You have hit the door, press space to open");
  DoorActive = true;
  }
  
  }else{
  DoorActive = false;
  }
  }
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
0
Best Answer

Answer by Tourist · Dec 13, 2012 at 10:15 AM

I think you forgot an else block. If the raycast hits something that is not tagged "Door", the flag DoorActive is not set to false.

If a first raycast hits the door then when you re moving around the raycast hits something else, the flag is true and you can be miles away from the door.

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 Fascination St · Dec 13, 2012 at 03:17 PM 0
Share

That did it! Thanks! I originally put an else block there, but then replaced it with the parent else block. It does make sense to have both. Thanks a lot!

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

10 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

Related Questions

Multiple Cars not working 1 Answer

Joystick Raycast Problem? 2 Answers

Straight Raycast Shooting 1 Answer

What is the best way of breaking boxes 1 Answer

JavaScript: Animation on button Help. 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