• 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 Jack Trades · Jul 30, 2012 at 04:01 PM · cameraraycast

Raycasting though ViewportPointToRay gives incorrect results

Hi, I'm using this piece of code to cast a ray though the center of the screen (including Debug code if I fucked it up somehow)

 var hit : RaycastHit;
  var ray = playerCamera.ViewportPointToRay(Vector3(0.5, 0.5, 0));
  if (Physics.Raycast(ray, hit)) {
  print(ray.origin);
  Debug.DrawRay(ray.origin, ray.direction * 100, Color.red, 5.0);
  Debug.Break();

The resulting rays origin seems to be off but direction is correct which results in ray hitting the stuff to the sides and/or below/above what it was actually supposed to hit.

I have absolutely no idea what I'm doing wrong here.

Thanks in advance.

Update: I just found found out that the more my camera is tilted, the more 'off' the origin of the ray is. My camera is a child of a complex character armature and I am modifying the camera's transform directly. Could any of those things be affecting ViewportPointToRay?

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 Mortoc · Jul 30, 2012 at 04:30 PM 0
Share

That seems to be correct, but just as a test, could you try:

 var ray = playerCamera.ScreenPointToRay(Vector3(Screen.width * 0.5, Screen.height * 0.5, 0));

That should be equivalent, but sometimes when doing weird stuff with the camera I've gotten ViewportPointToRay to get 'off'

avatar image Jack Trades · Jul 30, 2012 at 06:58 PM 0
Share

It didn't make any difference.

Update: I just found found out that the more my camera is tilted, the more 'off' the origin of the ray is. $$anonymous$$y camera is a child of a complex character armature and I am modifying the camera's transform directly. Could any of those things be affecting ViewportPointToRay?

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by gvaughan · Oct 01, 2019 at 08:27 PM

Just came across the same issue - Turns out the origin of the ray is based on the near clip plane of the camera, so it's off by at least that amount.

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

Answer by mooingduck · Jul 30, 2012 at 04:47 PM

It works just fine for me. Here is my test code:

 using UnityEngine;
 using System.Collections;
 
 public class ViewportRayTest : MonoBehaviour {
 
  public Vector3 point = new Vector3(0.5f, 0.5f, 0);
 
  public void Update() {
  Ray ray = Camera.main.ViewportPointToRay(point);
  Color col = Color.red;
  if(Physics.Raycast(ray)) col = Color.green;
  
  Debug.DrawRay(ray.origin, ray.direction*100, col);
  }
 }

Just open a blank scene, add a cube within view of the camera, and drop this script onto the cube. Slide the point around and the ray turns green when it hits the cube, or any other object with a collider.

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycast hitting objects to the left of my player 1 Answer

Raycast based on crosshair on screen 1 Answer

Raycast and First Person Controller 2 Answers

Raycast in camera 1 Answer

How to rotate a camera like Google Earth? 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