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
2
Question by henrypuspurs · Jun 20, 2013 at 08:55 PM · c#raycasting

Raycasting out from a first person controller

As I posted before I was trying to get OnMouseOver to do this for me but it wasnt happening, after looking into Raycasting I still cant get it to work. I want the raycast to tell me if the crosshair (GUI Crosshair at centre screen, locked to mouse position) is both pointing at and the player is close enough to an object, the intention is for this to then tell another script whether it should work (if statement checking the boolean of the ray) and also turn the crosshair another colour.

The issue I am having is getting the Ray to follow the camera on the First Person Controller. The public GameObject has been set to the main camera. The Debug is showing the ray stationary at a point away from the camera.

Please be aware I have no programming experience bar a couple of previous very simple scripts I did last week.

Here is how far I got with the Raycasting script.

 public class CrosshairRay : MonoBehaviour 
 {
     public float interactionDistance = 3f;
     public bool imnear;
     public GameObject cam;
     
     void Update ()
     {
 
     RaycastHit hit;
         Ray pointer = new Ray(cam.transform.localPosition, Vector3.forward);
         
         Debug.DrawRay(cam.transform.localPosition, Vector3.forward * interactionDistance);
         
         
         if(Physics.Raycast (pointer, out hit, interactionDistance))
             {
                 if(hit.collider.tag == "Clickable")
                 {
                     imnear = true;
                 }
                 else
                 {
                     imnear = false;
                 }
                     
             }    
     }    
     
 }
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 henrypuspurs · Jun 20, 2013 at 08:54 PM 0
Share

Also note the script is attached to the camera Object.

avatar image AlucardJay · Jun 20, 2013 at 11:25 PM 0
Share

A ray is defined with an origin and a direction.

The origin is in World-Space, you have defined a local position, so the ray will never start from where you expect. Check your DrawRay where it is being drawn.

Vector3.forward is in World-Space, you want the direction the camera is facing, so theObject.transform.forward.

This is what you're after :

 Ray pointer = new Ray(cam.transform.position, cam.transform.forward);

  • Ray : http://docs.unity3d.com/Documentation/ScriptReference/Ray.html

  • Raycast : http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html

1 Reply

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by robertbu · Jun 20, 2013 at 11:32 PM

You want 'cam.transform.position'. If the camera does not have a parent, 'localPosition' will always be (0,0,0). Also you may want the direction to be 'cam.transform.forward' instead of 'Vector3.forward'. 'Vector3.forward' will work if the camera rotation is (0,0,0), but cam.transform.forward will work no matter what the rotation of the camera. Also, if this script is attached to the camera, there is no need of the 'cam' game object variable. And there is a form of Physics.Raycast() that takes a position and direction, so there is no need to build a Ray. Putting the changes together:

 public class CrosshairRay : MonoBehaviour  {
     public float interactionDistance = 3f;
     public bool imnear;
     
     void Update () {
         RaycastHit hit;
 
         Debug.DrawRay(transform.position, transform.forward * interactionDistance);
 
         if(Physics.Raycast (transform.position, transform.forward, out hit, interactionDistance)) {
             imnear = (hit.collider.tag == "Clickable");
         }    
     }  
 }
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 henrypuspurs · Jun 21, 2013 at 08:07 AM 0
Share

Thanks that works perfectly.

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

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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Distribute terrain in zones 3 Answers

Unity bug with ray casting? 0 Answers

Multiple Cars not working 1 Answer

Issue with Raycast Detection in Editor 3 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