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 Alienjesus · Mar 11, 2011 at 08:07 PM · cameraraycastperspectiveworldtoscreenpoint

Raycasting forward from screen perspective instead of game position.

Hi, I'm making a game where you can pick up and move objects when an object is placed in front of them. This object has a raycast attached which enables the movement script when it finds a moveable object. However, the Ray travels forwards in terms of game position, which means when you play the game, you actually have to be slightly left or right of the object for the ray to hit, which isnt very intuitive.

So basically, I want to shoot a ray forward from the object whilst moving forward in terms of the screen position. It needs to start from the object, not the camera.

I think I can do this with Camera.WorldToScreenPoint, but, being completely honest, after trying several different ways, I figured I honestly have no idea how to use that code. Can anyone help me figure it out?

Here's the Raycasting portions of my script, although it's pretty standard:

function Update () {

var fwd = transform.TransformDirection (Vector3.forward); //Defining the travel direction of the raycast vector var hit : RaycastHit; //If Raycast collides with something, that becomes the 'hit' //var illuminate : RaycastHit; var LayerMask = 1 << 9; //Interacts with 1 layer - Layer 9 - Moveable

//Draws ray vector in Scene view Debug.DrawRay(transform.position, fwd * 50, Color.green);

(Physics.Raycast (transform.position, fwd, hit, 50, LayerMask));

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 flaviusxvii · Mar 11, 2011 at 08:14 PM 0
Share

I read this three times and I have no idea what you're asking. I think the current camera transform.forward is what you want. But I can't be sure.

avatar image Mike 3 · Mar 11, 2011 at 09:05 PM 0
Share

Camera.current will usually be null when not using it at the right place (For editor stuff and image processing), Camera.main is generally what you want

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Mike 3 · Mar 11, 2011 at 08:28 PM

var fwd = Camera.main.transform.forward;
//snip
Physics.Raycast (transform.position, fwd, hit, 50, LayerMask);

You can store Camera.main in start if you prefer, so you don't need to look it up every time

Edit:

var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
ray.origin = transform.position;
if (Physics.Raycast(ray, hit, 50, LayerMask))
{
    //code
}
Comment
Add comment · Show 6 · 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 Alienjesus · Mar 11, 2011 at 09:27 PM 0
Share

I tried this, but it isn't what I meant. It draws the Ray from the camera ins$$anonymous$$d of the object the scripts attached too. It seems I've not been clear so I'll try and explain again. I need the ray to be drawn from the object the script is attached to. However the ray needs to move straight forward in depth in terms of screen position rather than forward in the world space. $$anonymous$$y code generates the ray moving forward in world space which isn't directly forward in screen space, making it hard to deter$$anonymous$$e what its hitting or not.

Sorry if I'm unclear or whatever.

avatar image Mike 3 · Mar 11, 2011 at 09:30 PM 0
Share

just change Camera.main.transform.position to transform.position, rest stays the same

avatar image Mike 3 · Mar 11, 2011 at 09:31 PM 0
Share

edited the answer to make it a little more obvious :)

avatar image Alienjesus · Mar 11, 2011 at 09:39 PM 0
Share

Thanks, but it still doesnt seem to work. To pick up the object i need to go to the left or right of it depending which side of the screen its on. I want it to function more like if it was being clicked on by a mouse - so it only activates if the object that picks it up is directly in front of it from the cameras viewpoint. Your code seems like it's exactly what I need, so I'm a little confused why it's not working.

I appreciate the help though, thanks

avatar image Mike 3 · Mar 11, 2011 at 09:51 PM 0
Share

$$anonymous$$aybe something like the edit, though I'm kind of guessing using my interpretation of what your game is like ;)

Show more comments

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

No one has followed this question yet.

Related Questions

How to rotate a camera like Google Earth? 0 Answers

camera detecting walls 1 Answer

Correcting Object Offset on WorldToScreenPoint 0 Answers

Raycast hitting objects to the left of my player 1 Answer

Editor Camera Perspective 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