• 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 Der_Kevin · Dec 07, 2015 at 06:37 PM · raycastraycasthitray

set ray.point as x, y and z float

Hi! i am trying to get the x, y and z cordinates from a ray into a public float. the script looks like this:

 using UnityEngine;
 using System.Collections;
 
 public class RaytoMouse : MonoBehaviour {
 
     public float distance = 50f;
     public float x = 0;
     public float y = 0;
     public float z = 0;
     //replace Update method in your class with this one
     void FixedUpdate () 
     {    
         //if mouse button (left hand side) pressed instantiate a raycast
         if(Input.GetMouseButton(0))
         {
             //create a ray cast and set it to the mouses cursor position in game
             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
             RaycastHit hit;
             if (Physics.Raycast (ray, out hit, distance)) 
             {
                 //draw invisible ray cast/vector
                 Debug.DrawLine (ray.origin, hit.point);
                 //log hit area to the console
                 Debug.Log(hit.point);
                 
             }    
         }    
     }
 }

my question would be, how can i fill the public float x, y and z to the right coordinates? the debuging already works fine.

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 Cherno · Dec 07, 2015 at 07:34 PM

A Ray doesn't have a point variable. I assume you mean the point variable of the Hit type of class.

 if (Physics.Raycast (ray, out hit, distance)) 
              {
                  //draw invisible ray cast/vector
                  Debug.DrawLine (ray.origin, hit.point);
                  //log hit area to the console
                  Debug.Log(hit.point);
                   x = hit.point.x;
                   y = hit.point.y;
                   z = hit.point.z;
                  
              }    
   


Comment
Add comment · Show 1 · 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 Der_Kevin · Dec 07, 2015 at 08:47 PM 0
Share

perfect, thanks :)

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

33 People are following this question.

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

Related Questions

set ray only when raycast a specific layer 1 Answer

RaycastHit.transform/collider 1 Answer

Raycasting and foreach loops 1 Answer

[c#] While loop not exiting, I have no idea why 2 Answers

How to get information from raycast? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges