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
1
Question by nongenre · Jun 13, 2012 at 01:04 AM · raycastraycastingworldspacescreenspacescreenviewcoord

Raycast using an object in world space? (Converting to Screen Space?)

UPDATED - Thanks to a commenter pointing out a boneheaded mistake, I am re-wording this problem...

I've been pulling my hair out on this one - I am making a two-player game. Player one moves an object around a flat plane with the mouse. Player two moves around with the arrow keys.

I'm using Input.mousePosition to build a raycast for player one and it works great. I'm having no luck building a functioning raycast for player two, however. I know the difference between world and screen space, but when I try to convert the position of the player 2 object, my ray is pointing from the camera to some point in space.

Here's my (revised) code. I've tried several things that involve manipulating the math, but I'm about at my wit's end with it. Any input would be great. I think I just have yet to fully wrap my head around handling screen/world coords. NOTE - cursor_p2 is an object in the world which the player controls. Despite the name, it isn't a GUI object.

After correcting the badly-written Debug.DrawLine(), I now see that I'm much closer than originally anticipated. But as shown in the image, my raycast isn't accurate. It shoudl be pointing at the cursor object, but is hitting about 10 units off from it.

     // Try converting cursor world position to screen/pixel coords...
 p2Point = Camera.main.WorldToScreenPoint(cursor_p2.transform.position);

 var p2ray : Ray = Camera.main.ScreenPointToRay(p2Point);
         
 if (Physics.Raycast(p2ray.origin, p2ray.direction, p2hit)){
     // build some raycast info to feed children objects
     p2hitObj = p2hit.collider.gameObject;
     //Debug.Log("P2 ray hit " + p2hitObj.name);
     p2point = p2hit.transform.position;
 } else {
     Debug.Log("p2 raycast failed");
 }
 
 Debug.DrawLine(Camera.main.transform.position,p2hitObj.transform.position,Color.yellow);

![alt text][1] [1]: http://answers.unity3d.com/storage/temp/1242-Untitled.png

Untitled.png (323.9 kB)
Comment
Add comment · Show 6
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 Wolfram · Jun 13, 2012 at 01:10 AM 0
Share

Note your Debug.DrawLine() draws a line from the camera position (in world space) to the cursor position (in screen space), which doesn't make sense.

avatar image Wolfram · Jun 13, 2012 at 01:21 AM 0
Share

Also, we would need more information about cursor_p2 - is this a world space game object that simulates a cursor? How do the keys modify its position? Is the goal to move the corsur_p2 parallel to the screen using the keys?

avatar image nongenre · Jun 13, 2012 at 01:28 AM 0
Share

cursor_p2 is a prefab in the world - basically a textured plane which is simply translated 10 units (the size of the game plane's grid) in a direction when the corresponding arrow key is hit.

I don't need the raycast for movement (though I may change that) - but I need to figure out what the "cursor" object is over, so I planned on using this to do a filtered raycast for my terrain units.

avatar image Wolfram · Jun 13, 2012 at 01:35 AM 0
Share

I still don't get it. So your cursor_p2 is a plane parallel to your game grid (or is it a Unity terrain which also can vary in height?) and you want to cast a ray from the camera through cursor_p2 to figure out which object on your grid is being hit?

avatar image Mizuho · Jun 13, 2012 at 01:38 AM 1
Share

Sounds like somebody needs a screenshot. Screenshot it. And edit the details of your question onto it (helps more than a bad explanation).

Show more comments

2 Replies

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

Answer by Wolfram · Jun 13, 2012 at 12:29 PM

Hm, should have answered this immediately, but the solution was so simple, so I thought you were trying to do something different :-D

Just forget about the screenspace stuff. The ray you're looking for is simply the line from your camera position to the cursor_p2 position, both in world spacce:

 if (Physics.Raycast(Camera.main.transform.position,
                     cursor_p2.transform.position - Camera.main.transform.position))
 {
     p2point=cursor_p2.transform.position;
 }

Make sure the prefab's pivot(="origin") really is the center of your plane, not some point between your prefab's light and the plane, or something else.

EDIT: if you're interested in the actual intersection with your grid, you can also include the p2hit info. In this case, p2hit.point will contain the true intersection point, and p2hit.transform.position will contain the center(=pivot) of the object that was hit. All this assuming that cursor_p2 has no collider, or is not in a layer that can be hit by the Raycast of course.

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 Owen-Reynolds · Jun 13, 2012 at 06:47 AM

Your DrawLine is still off from where the ray is going. Use p2hit.point for the endpoint.

The problem is, suppose the ray hits cow C in the head. You draw the line to C.position, which is the cow's origin. If you ever hit the terrain, the Line goes completely off, to the origin of the terrain -- (0,0,0).

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

6 People are following this question.

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

Related Questions

How to get 3D worldspace position from 2D touch/mouse position 3 Answers

Diagonal Raycasting to detect platform returns true, even when false 1 Answer

Raycast hits and then doesn't fire again? 2D Game Raycasting Problem 1 Answer

My Raycasts seem to sometimes miss 0 Answers

Raycast not hitting the collider properly it always has a weird offset 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