x


How Do You Change The Ray Casting Position or Angle?

Howdy, I want to be able to use Ray Casting in an FPS so that when my FPC walks near to an object on the floor an animation plays. My problem is that with my current code the ray casting only connects with objects directly in-front of the player. Can anybody tell me how to change the position/angle of my ray casting so that it will be colliding with the floor a certain distance in front of my player.

Here is the code I'm currently working with.

function Update()
{
    var hit : RaycastHit;

    //check if we're colliding
    if(Physics.Raycast(transform.position, transform.forward, hit, 5))
    {
        //...with a plane
        if(hit.collider.gameObject.tag == "orangeplane")
        {
            //change colour!
            hit.collider.gameObject.animation.Play("orange");
        }
    }
}       

Any suggestions on how to do this would be greatly appreciated, Will.

more ▼

asked May 01 '10 at 08:24 PM

Will 1 gravatar image

Will 1
77 9 9 10

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

1 answer: sort voted first
Vector3 offset = new Vector3(0,2,0);
Vector3 dir = new Vector3(0,-2,5).normalized;
Physics.Raycast(transform.position + offset, dir, hit, 5)

This will cast a ray from 2m above your position through a point on the ground 5m in front of you.

more ▼

answered May 01 '10 at 11:18 PM

StephanK gravatar image

StephanK
6k 39 53 93

(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:

x1880
x1707
x1535

asked: May 01 '10 at 08:24 PM

Seen: 2291 times

Last Updated: May 01 '10 at 08:24 PM