- Home /
Obtain a vector3 point in space from a ray hit
forst srry for my english,
im trying to get a point in the space from a ray, but im casting the ray from a touch, so i seach in the scrpt reference and get this code:
for (var i = 0; i < Input.touchCount; ++i) {
if (Input.GetTouch(i).phase == TouchPhase.Began) {
// Construct a ray from the current touch coordinates
var ray = Camera.main.ScreenPointToRay (Input.GetTouch(i).position);
if (Physics.Raycast (ray)) {
//do something
}
}
i try to use: RaycastHit.Point for get the point in the space, but this dosnt work...
any can help me whit this?
i really dont understan this line of code, and there is the problem:
if (Physics.Raycast (ray)) {
thanks
Answer by GutoThomas · Apr 24, 2012 at 08:24 PM
You should define the origin and direction of the ray you've created otherwise the raycast will not have a start and a direction Vector3. Also, you should use a RaycastHit in order to obtain the collision details.
The code will look like so:
for (var i = 0; i < Input.touchCount; ++i) {
if (Input.GetTouch(i).phase == TouchPhase.Began) {
// Construct a ray from the current touch coordinates
var ray = Camera.main.ScreenPointToRay (Input.GetTouch(i).position);
var collider: RaycastHit;
if (Physics.Raycast (ray.origin, ray.direction, collider)) {
var collisionPoint: Vector3 = collider.point; // this will hold the exact position of the collision
}
}
Your answer
Follow this Question
Related Questions
This code only works on 3D Colliders and I want it to work on 2D. What do I need to change? 1 Answer
Raycast on touch 3 Answers
Problem With Raycast Pickup Using Touch 0 Answers
Touch to move 3 Answers
Layermask doesn't seem to work 2 Answers