Search Unity

SOLVED: Turning the direction of a raycast with the object

Discussion in 'Scripting' started by wabuilderman, Jul 31, 2014.

  1. wabuilderman

    wabuilderman

    Joined:
    Jul 30, 2014
    Posts:
    46
    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.

    Code (JavaScript):
    1. #pragma strict
    2. var contact : boolean;
    3. function Start () {
    4.  
    5. }
    6.  
    7. function Update () {
    8.     var hit : RaycastHit;
    9.     if(Physics.Raycast(transform.position, transform.up, hit, 10))
    10.     {
    11.         contact = true;
    12.     }
    13.     else
    14.     {
    15.         contact = false;
    16.     }
    17. }
    most of this seems self explanatory, but i just need to know how to get the local "up" rather than the global.
     
  2. mafiadude1

    mafiadude1

    Joined:
    Jun 28, 2012
    Posts:
    59
    Is this script on the actual sword GameObject or is it on a parent object?
     
  3. wabuilderman

    wabuilderman

    Joined:
    Jul 30, 2014
    Posts:
    46
    It is on the sword itself
     
  4. mafiadude1

    mafiadude1

    Joined:
    Jun 28, 2012
    Posts:
    59
    transform.up returns world space information. I believe you're looking for localPosition or localRotation.
     
  5. mafiadude1

    mafiadude1

    Joined:
    Jun 28, 2012
    Posts:
    59
    Or you can use inverseTransformDirection to convert it into local space.
     
  6. wabuilderman

    wabuilderman

    Joined:
    Jul 30, 2014
    Posts:
    46
    I tried those, and i even tried doing transform.localrotation.eulerangles, but it still didn't work
     
  7. mafiadude1

    mafiadude1

    Joined:
    Jun 28, 2012
    Posts:
    59
    Try using something like this

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

    wabuilderman

    Joined:
    Jul 30, 2014
    Posts:
    46
    It didn't work... here is my current code now: (I am using debug.drawRay to visualize it)
    Code (JavaScript):
    1. #pragma strict
    2. var contact : boolean;
    3. var up : Vector3;
    4. function Start () {
    5.  
    6. }
    7.  
    8. function Update () {
    9.     var hit : RaycastHit;
    10.     up = transform.TransformDirection(Vector3.up);
    11.     if(Physics.Raycast(transform.position, up, hit, 10))
    12.     {
    13.         contact = true;
    14.     }
    15.     Debug.DrawRay (transform.position, up, Color.green);
    16. }
     
  9. Aedous

    Aedous

    Joined:
    Jun 20, 2009
    Posts:
    244
  10. wabuilderman

    wabuilderman

    Joined:
    Jul 30, 2014
    Posts:
    46
    ok, so, It didn't work, BUT using trasnformDirection, I managed to achieve my goal.

    here is the code, for anyone who wants it:
    Code (JavaScript):
    1.  
    2. #pragma strict
    3. var contact : boolean;
    4. var up : Vector3;
    5. function Start () {
    6.  
    7. }
    8.  
    9. function Update () {
    10.     var hit : RaycastHit;
    11.     up = transform.TransformDirection(1,0,0);
    12.     if(Physics.Raycast(transform.position, up, hit, 10))
    13.     {
    14.         contact = true;
    15.     }
    16.     Debug.DrawRay (transform.position, up, Color.green);
    17. }