Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

making a vector from the raycast information

Discussion in 'Scripting' started by spiralgear, Feb 29, 2008.

  1. spiralgear

    spiralgear

    Joined:
    Dec 13, 2007
    Posts:
    528
    im raycasting at a wall, and i want to make a vector at the point the contact happened but a little bit off the wall


    so i did this
    Code (csharp):
    1.    
    2.     function Update () {   
    3.     var hit : RaycastHit;
    4.  
    5.    
    6.     if (Physics.Raycast (transform.position, fwd,hit,30)) {
    7.  
    8.      vec= Vector3(hit.point.x+5,hit.point.y,hit.point.z);  
    9.     }
    10.      


    and that works if im doing it on one side, but if i try to do it on the other side it goes 3 into the wall, instead of 3 in front of it


    I get why this is happening but i don't know how to fix it
     
  2. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Try this:

    Code (csharp):
    1. function Update ()
    2. {
    3.      var hit : RaycastHit;
    4.      if (Physics.Raycast (transform.position, fwd, hit, 30))
    5.      {
    6.           vec = hit.point;
    7.           vec += hit.normal.normalized * 5.0;
    8.      }
    9. }
     
  3. spiralgear

    spiralgear

    Joined:
    Dec 13, 2007
    Posts:
    528
    that works perfect, thanks alot
     
  4. mia

    mia

    Joined:
    Dec 31, 2007
    Posts:
    83
    just following this thread and curious if this line has a typo:
    vec += hit.normal.normalized * 5.0;

    ..was that supposed to be a + sign there instead of *?
     
  5. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Nope. It's supposed to be be a *. Does the * not work for you?
     
  6. 64746c

    64746c

    Joined:
    Oct 21, 2007
    Posts:
    181
    That line actually does move the starting point 5 units along the surface normal.
    hit.normal.normalized returns the normal vector with a length of 1, multiply that by five, add to hit.point vector.