- Home /
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
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;
}
Your answer
Follow this Question
Related Questions
Raycast returns null for no apparent reason 0 Answers
Need Help with RayCast. No Vector? 1 Answer
Raycast goes through 1 Answer
Shoot off multiple raycasts from 1 object? 2 Answers
Raycasting and foreach loops 1 Answer