Raycast Question

Ok so I'm learning the basics of raycasting and I want to draw a line from the "first person controller" to the hit point when I hit the use key. At the moment all that is happening is that it's logging with the following detail: "Something Hit at point (0.0,0.0,0.0)" and no line is drawn (none that I can see anyway)

I've added the script to the FPS prefab, no joy, tried an empty gameobject as a child which is a bit out from the CapsuleCollider, still nothing. It's probably something really simple

Here's the code:

function Update () {
if (Input.GetButtonUp("Use")){
    var fwd = transform.TransformDirection (Vector3.forward);

    var hit : RaycastHit;
    if (Physics.Raycast(transform.position, fwd, 10)) {

        Debug.DrawLine(transform.position, hit.point);
        Debug.Log("Something Hit at point" + hit.point);
        }

      }

}

Please let me know if you can help. Thanks...

You don't need this:

var fwd = transform.TransformDirection (Vector3.forward);

You can just use transform.forward instead.

As for your problem, make sure the ray isn't intersecting the player collider (you should be able to address this using layers, e.g. by putting the player collider in the 'ignore raycast' layer).

If the camera is associated with the object that's doing the raycasting, you still may not be able to see the line (because it will be reduced to a point when rendered). But, you could instead draw a box or a sphere at the hit point, which you should be able to see.

Thanks Jesse, I knew about the transform.forward thing I just copied that line from another script and never changed it.

The reason I was getting no data back was I forgot to put in the extra var for 'hit' i.e.

Physics.Raycast(transform.position,transform.forward, hit,  10)

I also had a problem where I couldn't see the lines being drawn, but I just went with DrawRay and then finally figured out it only shows in Scene view not game view.

In fact, in the end I learned about Physics.SphereCast and used that instead for checking if objects are in front of camera for 'use'...

Here's my latest (basic) code:

var MinimumPickupDistance : float = 1.5;
var TheInventory: String;

function Update () {

Debug.Log("Inventory:" + TheInventory);

if (Input.GetButtonDown("Use")){
    var hit: RaycastHit;

    var charContr : CharacterController = GetComponent(CharacterController);
    var p1 : Vector3 = transform.position + charContr.center +
    Vector3.up * (-charContr.height*0.5);
    var p2 : Vector3 = p1 + Vector3.up * charContr.height;

    if (Physics.CapsuleCast (p1, p2, charContr.radius, transform.forward, hit, MinimumPickupDistance)) {
        distanceToObstacle = hit.distance;

        //Print out distance to object no matter what
        Debug.Log("distance to Obstacle = "+distanceToObstacle);

        //--------------------------------------------
        // Code for Picking Up Inventory Objects
        if (hit.collider.gameObject.tag == "pickupable"){
            theObject = hit.collider.gameObject;
            Destroy(theObject);
            Debug.Log("Item Picked Up");
            TheInventory += theObject.name + ",";
        }
        //--------------------------------------------
        // Code for putting objects into recepticles
        if (hit.collider.gameObject.tag == "recepticle"){

        }
        //--------------------------------------------

    }

}

}

I'm going to work on proper inventory system next with hashtables or maybe an SQLite database.

It seems that I posted this question a bit prematurely but I was getting worried I wouldn't figure it out.

Thanks for your help.