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

ray to object trouble!

Discussion in 'Editor & General Support' started by elias723, May 25, 2006.

  1. elias723

    elias723

    Joined:
    Feb 10, 2006
    Posts:
    178
    Hey everyone - I have a script which fires rays, and if the rays hit anything, the thing moves (think bullet). The problem is, my character has a rigidbody but it is kinematic so that my moving script will work. How can I get to a script on the thing that the ray hits to detriment his health?

    The part of this not in "code form" is my attempt at a fix...
    Here's my script for firing:
    Code (csharp):
    1.  
    2. var Force = 50;
    3. var Detriment = 10;
    4.  
    5. var Tracer : GameObject;
    6. var GunpointFire : GameObject;
    7. var TracerDestroySeconds = 1.0;
    8. var GunpointFireDestroySeconds = 0.1;
    9. var Cam : Camera;
    10.  
    11. var fireRate = 0.1;
    12. private var nextFire = 0.0;
    13.  
    14. function Update () {
    15.     if (Input.GetButton ("FireL1")  Time.time > nextFire) {
    16.         nextFire = Time.time + fireRate;
    17.         ray = Cam.ViewportPointToRay(Vector3(0.5, 0.5, 0.0));
    18.         var hit : RaycastHit;
    19.         if (Physics.Raycast (ray, hit)) {
    20.             target = hit.point;
    21.         }
    22.         else {
    23.             target = (ray.origin + ray.direction * 1000);
    24.         }
    25.         direction = (transform.position - target).normalized;
    26.         if(hit.rigidbody) {
    27.             StartCoroutine("ApplyForce", hit.rigidbody);
    28.         }
    29.  
    if(hit.collider.gameObject.GetComponent(Life) hit.collider.gameObject.GetComponent(Life).Life > 0) {
    hit.collider.gameObject.GetComponent(Life).ChangeHealth(-Detriment);
    }

    Code (csharp):
    1.        
    2.         if(GunpointFire) {
    3.             var gpfire : GameObject = Instantiate (GunpointFire, transform.position, Quaternion.FromToRotation(Vector3.fwd,direction));
    4.             Destroy (gpfire, GunpointFireDestroySeconds);
    5.         }
    6.    
    7.         if(Tracer) {
    8.             var trace : GameObject = Instantiate (Tracer, transform.position, Quaternion.FromToRotation (Vector3.fwd, direction));
    9.             var x = 0.0;
    10.             while( x <= 1.0) {
    11.                 trace.transform.position = Vector3.Lerp(transform.position, target, x);
    12.                 x+=.2;
    13.             }
    14.             Destroy (trace, TracerDestroySeconds);
    15.         }
    16.     }
    17. }
    18.  
    19. function ApplyForce( rb ) {
    20.     var time = 0.01;
    21.     while(time > 0  rb) {
    22.         direction = rb.transform.position - transform.position;
    23.         direction.y += 10;
    24.         direction = direction.normalized;
    25.         rb.AddForce(Force * direction);
    26.        
    27.         yield new WaitForFixedUpdate ();
    28.         time -= Time.deltaTime;
    29.     }
    30. }
    31.  
    The area which I am trying to fix this problem is in bold and underlined. Thanks for all your help!
     
  2. elias723

    elias723

    Joined:
    Feb 10, 2006
    Posts:
    178
    I found a way - but it's a little lame. I just did the same sort of thing as in an explosion script, ie made an enclosing sphere that would only enclose one player at a time. From that I could get to the script through the collider in the sphere. Thanks anyways.
     
  3. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    Did you use the same line as above that is bolded and underlined? Or did you need to get a handle on he script first? Ex:
    Code (csharp):
    1.  
    2. yourScript = hit.collider.gameObject.GetComponent(Life);
    3.  
    I can't always keep it straight how dynamic the Javascript to .Net implementation is.
     
  4. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    I use this for my bullet scripts:

    Code (csharp):
    1. script = hit.collider.gameObject.GetComponent(Life);
    2.  
    3. if(script) script.life -= damage;
     
  5. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    btw. you don't need the extra game object property.

    Code (csharp):
    1.  
    2. script = hit.collider.GetComponent(Life);
    3. if(script) script.life -= damage;
    4.  
     
  6. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    Oh ok. Thanks.