Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to rotate Raycasts?

Discussion in 'Scripting' started by Fuby, Nov 18, 2014.

Thread Status:
Not open for further replies.
  1. Fuby

    Fuby

    Joined:
    Mar 26, 2014
    Posts:
    35
    I need the distance from my object to the target hit, for another script. The problem is, that my object rotates while my raycast keeps facing down. How can i rotate my raycast or tell it to rotate along with the (parent)object?



    Code (JavaScript):
    1.  
    2. #pragma strict
    3.  
    4.     var _distance : float;
    5.     var _direction : Vector3;
    6.  
    7.     function FixedUpdate ()
    8.     {
    9.         var _hit:RaycastHit;
    10.  
    11.         _direction = Vector3.down;
    12.         _direction = Quaternion.Euler(
    13.         transform.parent.rotation.x,
    14.         transform.parent.rotation.y,  
    15.         transform.parent.rotation.z)
    16.         * _direction;
    17.        
    18.         if (Physics.Raycast(transform.position, _direction, _hit, 10.0f))//Fall
    19.         {
    20.             Debug.Log(_hit);
    21.             _distance = (this.transform.position.y) -
    22.            (_hit.transform.position.y);
    23.             SendMessageUpwards ("FrontDownToWorldToCharacterMove", _distance);
    24.         }
    25.         Debug.DrawRay (transform.position, _direction, Color.green);
    26.     }
    27.  
     
  2. GetUpKidAK

    GetUpKidAK

    Joined:
    Apr 9, 2013
    Posts:
    84
    You could try using:

    Code (CSharp):
    1. _direction = -transform.up;
     
  3. cl9-2

    cl9-2

    Joined:
    May 31, 2013
    Posts:
    417
    One way is to transform the direction vector from local object space to world space, which can be done with Transform.TransformDirection.

    Code (JavaScript):
    1. #pragma strict
    2. var _distance : float;
    3. var _direction : Vector3;
    4. function FixedUpdate ()
    5. {
    6.     var _hit:RaycastHit;
    7.      
    8.       if (Physics.Raycast(transform.position, transform.TransformDirection(_direction), _hit, 10.0f)) //Fall
    9.     {
    10.          Debug.Log(_hit);
    11.         _distance = (this.transform.position.y) - (_hit.transform.position.y);
    12.         SendMessageUpwards ("FrontDownToWorldToCharacterMove", _distance);
    13.     }
    14.     Debug.DrawRay (transform.position, _direction, Color.green);
    15. }
    16.  
     
    praveen_unity660 likes this.
  4. Fuby

    Fuby

    Joined:
    Mar 26, 2014
    Posts:
    35
    Thanks, that worked for me! Never thought I´d be that simple...
     
    GarthSmith likes this.
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,414
    Please don't necro an 8 year old post like this. There's a like button if you need to show your appreciation of a post.
     
Thread Status:
Not open for further replies.