x


how to get info on what raycast hitted

Basically, I wanted to determine whether there is any obstacle between Player and Target. And then return the target info. I tried to use Raycast, but feel free to offer alternative if you can provide one.

I managed to get the Raycast line stopped at where the collider is. But I don't know how to get the variables from the object which the collider is attached to.

Also, is there anyway to widen the ray? In case the player might not be able to pass through gap between objects.

Code so far:

public Transform Player;
public Transform TargetLocationFinal;

    void DrawRayCast()
    {
       RaycastHit hit = new RaycastHit();

       Vector3 direction = TargetLocationFinal.transform.position - Player.transform.position;

       if (Physics.Raycast(Player.transform.position, direction , out hit))
       {
           Debug.DrawLine (Player.transform.position, hit.point);
       }
    }
more ▼

asked Jun 05 '12 at 10:49 PM

Levantez gravatar image

Levantez
38 9 18 22

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

All the information is in your RaycastHit. you have access to the collider, and from there everything with GetComponent.

You can't widen the ray, but you can cast several by spacing them of transform.right, left, up etc.

more ▼

answered Jun 05 '12 at 10:50 PM

Berenger gravatar image

Berenger
11k 12 19 53

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1687
x1528
x193
x151

asked: Jun 05 '12 at 10:49 PM

Seen: 493 times

Last Updated: Jun 05 '12 at 10:50 PM