• 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 oliver-jones · Feb 07, 2011 at 04:10 PM · raycentercastinitiate

Initiate RayCast From Center of Camera?

Hello,

How would I go about Initiating a ray cast from the center of the screen (camera). Right now I am initiating from the mouses position:

function Update () {

 var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 var hit : RaycastHit;
 if(Physics.Raycast (ray, hit, 500))
     Debug.DrawLine (ray.origin, hit.point);
     var objectTouching = hit.collider.name;
     print(objectTouching);
     currentObjectOver = hit.collider.gameObject;

}

Thanks,

Ollie

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

2 Replies

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

Answer by Bunny83 · Feb 07, 2011 at 04:28 PM

It's quite easy since that's the "normal" way to raycast:

var cam : Transform = Camera.main.transform;
if(Physics.Raycast (cam.position, cam.forward, hit, 500))

or if you need the Ray use:

var cam : Transform = Camera.main.transform;
var ray = new Ray(cam.position, cam.forward);
var hit : RaycastHit;
if(Physics.Raycast (ray, hit, 500))
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 SimpleGiant · Jan 30, 2014 at 02:25 PM 0
Share

This is not working for me, I am getting this error.

UnityException: You are not allowed to call this function when declaring a variable. $$anonymous$$ove it to the line after without a variable declaration.

is this cod C# or JavaScript? im running Java as main.

avatar image Bunny83 · Jan 31, 2014 at 02:58 AM 0
Share

@SimpleGiant:
This is of course UnityScript (Unity's Javascript dialect) since the question was using it as well. This code works pretty well inside a function of course. You probably used it outside a function where you usually declare your member variables.

I suggest you watch some very basic coding tutorials about scripts and classes. If you don't know that a declaration is and what's executable code you won't get anywhere.

avatar image
9

Answer by Simple · Feb 07, 2011 at 04:16 PM

 var ray : Ray = Camera.main.ViewportPointToRay (Vector3(0.5,0.5,0));   
        var hit : RaycastHit;
        Physics.Raycast (ray, hit);

Vector3(0.5,0.5,0) uses x = 0.5 and y = 0.5 because Viewport vectors are normalized(which means they take value from 0 to 1). So, it will be the middle of camera viewport.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

[SOLVED] Raycast for custom button JS 1 Answer

How to get information from raycast? 2 Answers

Raycast intersection 1 Answer

How to get a Vector3 from a RaycastHit variable? 1 Answer

destroy gameobject with ray 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