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 3 · Feb 18, 2013 at 02:07 AM · cameraiosraycastrayscreenpointtoray

Why is the camera.screenpointtoray off?

There arent any errors that prevent me from playing, and it seems to work, but the ray is really off. I have found that I have to move the camera 90 degrees to the right for it to allow me to pick it up. Any suggestions?

 #pragma strict
 
 function Start () {
 
 }
 var guiray : GUIText;
 var flashlight : GameObject;
 var flashlightdesk : GameObject;
 function Update () {
       var hit : RaycastHit;
 var ray = camera.ScreenPointToRay (Vector3(200,200,0));
      Debug.DrawRay (ray.origin, ray.direction * 10, Color.yellow);
        var fwd = transform.TransformDirection (Vector3.forward);
 if (Physics.Raycast(ray, hit, 10)){
      //   print ("There is something in front of the object!");
   //  var fingerCount = 0;
  //   for (var touch : Touch in Input.touches) {
  //       if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
    //         fingerCount++;
  //   }
   //      if (fingerCount > 0){
   //      print ("User has " + fingerCount + " finger(s) touching the screen");
  if(hit.collider.gameObject.CompareTag("light")) {
 guiray.text = "Pick Up Flashlight"; 
  //   if (Input.touchCount > 0) {
 if (Input.GetButtonDown("Fire1")){
    flashlight.gameObject.active = true;
       //flashlight.gameObject.enabled = true;
    flashlightdesk.gameObject.active = false;
 }
 }
 else {
 guiray.text = "";
 }
 }
 }
 
  
Comment
Add comment · Show 1
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 Benproductions1 · Feb 18, 2013 at 03:05 AM 0
Share

Please propery indent your code, it's looks horrible right now :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Feb 18, 2013 at 03:55 AM

As @Benproductions1 mentioned, please clean up your code before posting it to the list. You are far more likely to get answers if the code is readable. With that said, I pasted your code into a file and made some changes so it would run. When I examined the raycast, it was exactly as I expected. Perhaps you need to take a screen shot of the scene view so that we can see what you are seeing. The one major change I made to the code was to explicitly use Camera.main rather than the camera attached to the game object. Do you have multiple camera in your project? Here is the code I tested:

 //var guiray : GUIText;
 //var flashlight : GameObject;
 //var flashlightdesk : GameObject;
 
 function Update () {
     var hit : RaycastHit;
     var ray = Camera.main.ScreenPointToRay (Vector3(200,200,0));
 
     Debug.DrawRay (ray.origin, ray.direction * 10, Color.yellow);
     var fwd = transform.TransformDirection (Vector3.forward);
     
     if (Physics.Raycast(ray, hit, 10)){
         if(hit.collider.gameObject.CompareTag("light")) {
             //guiray.text = "Pick Up Flashlight"; 
 
             if (Input.GetButtonDown("Fire1")){
                  //flashlight.gameObject.active = true;
 
                  //flashlightdesk.gameObject.active = false;
             }
         }
         else 
         {
             //guiray.text = "";
         }
     }
 }
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

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

Follow mouse cursor (with Y = 0)? 1 Answer

Get 2D Collider with 3D Ray 2 Answers

Not getting right value; ScreenPointToRay 0 Answers

How to make a ray always shoot forward from Camera viewpoint. 1 Answer

How do I keep functionality of LookAt script whilst using it in a 3dGUI camera layer? 0 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