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 Joseph Lerner · Jul 29, 2014 at 05:25 AM · c#raycastraypointprint

Setting a point with a ray C#

I have finally figured what a ray is and how to make one in C#. The whole reason why I made the ray, however, was to be able to set my mouse location as a point in a 3D enviroment.

So... how would I use my ray to set a point

Before you get made at me for not looking on the scripting reference, I am able to say in my defense that i found something that possibly could be what I'm looking for. Unfortunately it didn't come with an explanation of what it was.

     print(ray.GetPoint(10));

Here is my first script that has the ray following my mouse pointer.

 using UnityEngine;
 using System.Collections;
 
 public class Raycast : MonoBehaviour 
 {
     
     void Update () 
     { Ray ray = camera.ScreenPointToRay(new Vector3( Input.mousePosition.x, Input.mousePosition.y , Input.mousePosition.z));
         Debug.DrawRay(ray.origin, ray.direction * 100, Color.red);
     }
 }
 


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

1 Reply

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

Answer by robertbu · Jul 29, 2014 at 05:34 AM

Imagine you are standing at a window looking out at your yard and some mountains in the distance. Image you put finger on the glass a bit to the right of you at eye level and sight along the tip of your finger. If what you sight is close to the window, it might be only a few feet to the right. But if your finger landed on the mountains in the distance, the position your finger indicates might be miles to your right. That is, where an object is in 3D space is highly dependent on the distance that object is from the viewing plane.

Ray.GetPoint() gets a point the specified distance from the origin along the ray. It can work to translate a mouse position into a world position. The one thing to note is that Camera.ScreenPointToRay() returns a a ray that starts at the camera near clipping plane, not at the camera. So if you do:

  Vector3 pos = ray.GetPoint(10);

You are finding a point a bit further than 10 units from the camera.

An alternate method of doing what you are attempting here is to Camera.ScreenToWorldPoint(). The 'z' of the Vector3 you pass to this function is the distance in front of the camera you want to use to generate the point.

A bit of a nit, but you don't need create a new Vector3 from the mousePosition. Input.mousePosition is already a Vector3. So you can do:

 Ray ray = camera.ScreenPointToRay(Input.mousePosition);
Comment
Add comment · Show 3 · 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 Joseph Lerner · Jul 29, 2014 at 04:05 PM 0
Share

In this picture below I marked my point 10 units along the ray with a line to make sure it was there. Now I have a point in my ray.

alt text][2]

Would it be possible to make my ray set the point on contact with a Gameobject? That way my point would always be on the floor of my scene, never under or over.

a.png (270.9 kB)
avatar image robertbu · Jul 29, 2014 at 04:33 PM 0
Share

Figure out the 'point on contact' uses some form of Raycasting(). See Physics.Raycast(), Collider.Raycast(), and Plane.Raycast(). Which one to use will depend on your situation.

avatar image Joseph Lerner · Jul 29, 2014 at 05:07 PM 0
Share

Ok thanks a lot for the help.

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

2 People are following this question.

avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

For each Raycast? 1 Answer

Distribute terrain in zones 3 Answers

how do i fix this small error 2 Answers

How to detect if a raycast ray stop hitting an object 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