Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

raycast troubles

Discussion in 'Scripting' started by morgansaysthis, Mar 27, 2007.

  1. morgansaysthis

    morgansaysthis

    Joined:
    Jan 12, 2007
    Posts:
    245
    can some one tell me why this isnt working


    Code (csharp):
    1.  
    2. var rangevar=100;
    3.  
    4.  
    5. function Update () {
    6.  
    7.     var hit : RaycastHit;
    8.  
    9.     var fwd = transform.TransformDirection (Vector3.forward);    
    10.    
    11.     if (Physics.Raycast (transform.position, fwd, rangevar)) {
    12.          
    13.            
    14.        
    15.         print ("hitsomething");
    16.        
    17.         if(hit.rigidbody)
    18.        
    19.        
    20.         hit.rigidbody.AddForceAtPosition(10.0 * fwd, hit.point);
    21.        
    22.        
    23.        
    24.        
    25.        
    26.        
    27.        
    28.        
    29.     }
    30. }

    i have that on a rigidbody FPS, and when the lvl starts theres a rigidbody with a bx collier in front of him, im trying to get it to move away from my char, im not getting an error, but the things not moving



    i was doing this just to try to figure out how raycasting works so while im here ill just ask you guys, am i correct to assume


    a.when it says "hit" in the code after the raycast, its refering to the object that was hit

    b.once an object has been hit, my char stops raycasting

    c.if hit does refer to the object that was hit, can i get its instance ID and change var's with in it


    thanks
     
  2. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    You forgot to assign a value to the hit variable. You have to pass it as the third parameter to Physics.Raycast as in the code below:
    Code (csharp):
    1. var rangevar=100.0;
    2. function Update () {
    3.  
    4.     var hit : RaycastHit;
    5.     var fwd = transform.TransformDirection (Vector3.forward);      
    6.    
    7.     if (Physics.Raycast (transform.position, fwd, hit, rangevar)) {
    8.        
    9.         print ("hitsomething");
    10.        
    11.         if(hit.rigidbody)
    12.            hit.rigidbody.AddForceAtPosition(10.0 * fwd, hit.point);
    13.     }
    14. }
    15.  
     
  3. Willem

    Willem

    Joined:
    Mar 9, 2007
    Posts:
    184
    Good catch, I didn't even see that. Isn't there an error on Unity's status bar when you save that script saying that you have an unused variable? There should be.
     
  4. Bampf

    Bampf

    Joined:
    Oct 21, 2005
    Posts:
    369
    The variable IS being used. :)

    I believe in C# you'd get a warning that the variable may not be set before it's used. Perhaps Javascript is different?
     
  5. Willem

    Willem

    Joined:
    Mar 9, 2007
    Posts:
    184
    Oops, you're right! Well, at the very least I would expect a complaint about it not being initialized then.
     
  6. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    RaycastHit is a struct, so it is automatically initialised to a default value when declaring the variable.
     
  7. Willem

    Willem

    Joined:
    Mar 9, 2007
    Posts:
    184
    *sigh*

    I'll get my coat.
     
  8. morgansaysthis

    morgansaysthis

    Joined:
    Jan 12, 2007
    Posts:
    245
    ahhh i see, thanks alot