Search Unity

Another one not quite working ...

Discussion in 'Scripting' started by DaveyJJ, Jun 18, 2007.

  1. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    In this case, I instantiate the ball fine, but the last lines where I set the BallInPlay to true isn't being regsitered and I can fire more balls every time I hit and release the mouse (I am getting better at this coding stuff though!) ...

    Code (csharp):
    1.  
    2. /* ----- variables for gameplay ----- */
    3. // identifies the ball object
    4. var projectile : Rigidbody;
    5.  
    6. // variables to define how hard the ball gets kicked and how long it exists
    7. // will need to add chargepower variable later
    8. var BallSpeed = 10;
    9.  
    10. // a boolean that will be set to true or false to indicate if a ball is currently in play
    11. var BallInPlay : boolean;
    12.  
    13.  
    14. // we start the function when this comes alive setting BallInPlay to false to allow us to fire a ball
    15. function Start () {
    16.     BallInPlay == false;   
    17. }
    18.  
    19. function Update () {
    20.    if (Input.GetMouseButtonUp (0)  BallInPlay == false) {
    21.         KickBall();
    22.    }
    23. }
    24.  
    25. function KickBall() {
    26.    
    27.         ray = Camera.mainCamera.ScreenPointToRay (Input.mousePosition);
    28.         var hit : RaycastHit;
    29.  
    30.         //if (audio  !audio.isPlaying) {
    31.         //    audio.Play ();
    32.         //}
    33.  
    34.         // determines where to aim ball
    35.         if (Physics.Raycast (ray, hit)) {
    36.             target = hit.point;
    37.         }
    38.         else {
    39.             target = (ray.origin + ray.direction * 20);
    40.         }
    41.        
    42.         direction = target - transform.position;
    43.         var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, Quaternion.FromToRotation (Vector3.fwd, direction));
    44.         BallInPlay == true;
    45.         instantiatedProjectile.velocity = direction.normalized * BallSpeed;
    46.         yield WaitForSeconds (5);
    47.         instantiatedProjectile.active = false;    
    48.         BallInPlay == false;
    49.        
    50. }
    51.  
    Also, I was using this ...

    Code (csharp):
    1.  
    2. Destroy (instantiatedProjectile,5);
    3.  
    instead of this line ...

    Code (csharp):
    1.  
    2. instantiatedProjectile.active = false;
    3.  
    But "destroy" doesn't seem to remove the game object from the game, it simply freezes the ball and leaves it on the field as a collideable object (not desired). Will "instantiatedProjectile.active = false;" use up any extra CPU power or is the object gone forever?
     
  2. bronxbomber92

    bronxbomber92

    Joined:
    Nov 11, 2006
    Posts:
    888
    edit - I didn't see you set the BallInPlay twice. Your problem is what's mentioned below.
     
  3. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    I see that you are using "==" when you are not comparing anything, several times in your script. Maybe you should only use one "=" instead.
     
  4. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Also, what you are destroying is the projectiles rigidbody. That is why it is freezing. So you should probably say:

    Code (csharp):
    1. Destroy (instantiatedProjectile.gameObject);
     
  5. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    Removed the "instantiatedProjectile.active = false;" and replaced with above ... works fine thanks. Working on replacing the =='s now.

    yes! Fixed it. Working perfectly now.

    Now can I add a simple bit of code that gets the "instantiatedProjectile.velocity" if it's less than say 0.1 and THAT destroys the ball instead of a number of seconds?
     
  6. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Did you want something like this:

    Code (csharp):
    1. if (instantiatedProjectile.velocity.magnitude < 0.1) {
    2.      Destroy (instantiatedProjectile.gameObject);
    3.      BallInPlay = false;
    4. }
     
  7. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    That is the idea but it isn't working when placed inside the KickBall function. Should it instead go into the function Update to watch for the ball slowing down?
     
  8. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Put this under the kickball function instead.

    Code (csharp):
    1. while (instantiatedProjectile.velocity.magnitude > 0.1)
    2.     yield;
    3. Destroy (instantiatedProjectile.gameObject);      
    4. BallInPlay = false;


    So here's what the final script should look like:

    Code (csharp):
    1. var projectile : Rigidbody;
    2. var BallSpeed = 10;
    3. var ballInPlay : boolean;
    4.  
    5. function Start () {
    6.    ballInPlay = false;    
    7. }
    8.  
    9. function Update () {
    10.    if (Input.GetMouseButtonUp (0)  !ballInPlay)
    11.          KickBall();
    12. }
    13.  
    14. function KickBall() {
    15.     ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    16.     var hit : RaycastHit;
    17.     if (Physics.Raycast (ray, hit))
    18.         target = hit.point;
    19.     else
    20.         target = (ray.origin + ray.direction * 20);
    21.        
    22.     direction = target - transform.position;
    23.     var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, Quaternion.identity);
    24.     ballInPlay = true;
    25.     instantiatedProjectile.velocity = direction.normalized * BallSpeed;
    26.     while (instantiatedProjectile.velocity.magnitude > 0.1)
    27.         yield;
    28.     Destroy (instantiatedProjectile.gameObject);      
    29.     ballInPlay = false;
    30. }
     
  9. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    Beautiful Daniel, I will drop it in when I get home and let you know.