SOLVED: Turning the direction of a raycast with the object

I have a sword (using the suggestion i got from my previous thread) that raycasts, to find the point of collision. HOWEVER, it only checks in the global upward direction, rather than turning to face whatever direction my sword is oriented. Here is my code so far, just to see if i am indeed getting a registered collision.

#pragma strict
var contact : boolean;
function Start () {

}

function Update () {
    var hit : RaycastHit;
    if(Physics.Raycast(transform.position, transform.up, hit, 10))
    {
        contact = true;
    }
    else
    {
        contact = false;
    }
}

most of this seems self explanatory, but i just need to know how to get the local “up” rather than the global.

Is this script on the actual sword GameObject or is it on a parent object?

It is on the sword itself

transform.up returns world space information. I believe you’re looking for localPosition or localRotation.

Or you can use inverseTransformDirection to convert it into local space.

I tried those, and i even tried doing transform.localrotation.eulerangles, but it still didn’t work

Try using something like this

Vector3 up= transform.TransformDirection(Vector3.up);

It didn’t work… here is my current code now: (I am using debug.drawRay to visualize it)

#pragma strict
var contact : boolean;
var up : Vector3;
function Start () {

}

function Update () {
    var hit : RaycastHit;
    up = transform.TransformDirection(Vector3.up);
    if(Physics.Raycast(transform.position, up, hit, 10))
    {
        contact = true;
    }
    Debug.DrawRay (transform.position, up, Color.green);
}

Your problem is your using transform.TransformDirection instead of transform.InverseTransformDirection.

That should fix your problem :slight_smile:

ok, so, It didn’t work, BUT using trasnformDirection, I managed to achieve my goal.

here is the code, for anyone who wants it:

#pragma strict
var contact : boolean;
var up : Vector3;
function Start () {

}

function Update () {
    var hit : RaycastHit;
    up = transform.TransformDirection(1,0,0);
    if(Physics.Raycast(transform.position, up, hit, 10))
    {
        contact = true;
    }
    Debug.DrawRay (transform.position, up, Color.green);
}