Search Unity

ending a streak

Discussion in 'Scripting' started by Zou, Dec 11, 2007.

  1. Zou

    Zou

    Joined:
    Oct 19, 2007
    Posts:
    22
    Hi

    I’m working on a game where the player controls the camera, moving from side to side and throwing a ball at a receiver(s), also moving from side to side. I have defenders moving back and forth towards the player and if the player gets hit, they lose points. So, I have a score system and catch streak working but my problem is trying to stop the catch streak when the player misses the receiver.

    Here is the some of the code I’m using and what I have tried:

    This script is place on the ball prefab that will be instantiated

    Code (csharp):
    1. var velocity = 5;
    2. private var grounded = false;
    3.  
    4. function FixedUpdate ()
    5. {
    6.     transform.Rotate(0,0,10);
    7.     if (grounded == true )
    8.     {
    9.         transform.TransformDirection(Vector3.forward * 20);
    10.         transform.Rotate(20,0,0);
    11.     }
    12. }
    13.  
    14. function OnCollisionEnter()
    15. {
    16.     grounded = true;
    17.     Destroy(gameObject,1);
    18. }
    For this, I have tried to put a script within the ‘if(grounded == true)’ statement to call another script to end the catch streak but I could not update the inspector afterwards with catch GUIText.

    Another idea was to put a script on the ground plane to end the catch streak:
    Code (csharp):
    1. var catches : Score;
    2. var football : Rigidbody;
    3. function OnCollisionEnter ()
    4. {
    5.     catches.StreakDead();
    6. }
    But the problem that came up was that everytime the ball hit the ground it would end the streak.

    To solve this, I tried to destroy the ball when it hit the receiver by calling a function [like Dest()] within this code that is attach to the receiver :

    Code (csharp):
    1. var football : Rigidbody;
    2. var score : Score;
    3. var catches: Score;
    4. //var kill : footballRolling;
    5.  
    6. function OnTriggerExit ()
    7. {  
    8.     //kill.Dest();
    9.     catches.AddToStreak();
    10.     score.AddToScore ();
    11.     print("GOAL!!!!");
    12. }
    Or attached in the first code I posted but when play testing, it tells me the prefab has become null or that it can’t be destroyed.

    Here is the code I am using to throw the ball (instantiate the prefab):

    Code (csharp):
    1. var football : Rigidbody;
    2. var chargeTime = -1.0;
    3. var StrBar : GameObject;
    4. var MaxHold = 10.0;
    5. StrBar.transform.localScale.x = 0;
    6.  
    7. function Update ()
    8. {
    9.     //var autoDestroySeconds = 25;
    10.  
    11.     if (Input.GetMouseButtonDown(0))
    12.     {
    13.         chargeTime = Time.time;
    14.         StrBar.transform.localScale.x = 0;
    15.         var timeCount = Time.deltaTime;
    16.         if (timeCount > MaxHold)
    17.         {
    18.             timeCount = MaxHold;
    19.         }
    20.         else
    21.         {
    22.             StrBar.transform.localScale.x = 12 *timeCount/MaxHold;
    23.         }
    24.     }
    25.  
    26.     if (!Input.GetMouseButton (0)  chargeTime != -1.0)
    27.     {
    28.         var HoldTime = (Time.time - chargeTime);
    29.         var speed = 11 + 22 * (Time.time - chargeTime);
    30.         var speed2 = 2 + 4 * (Time.time - chargeTime);
    31.         if(HoldTime < MaxHold)
    32.         {
    33.             StrBar.transform.localScale.x = 12 * HoldTime/MaxHold;
    34.         }
    35.         else
    36.         {
    37.             StrBar.transform.localScale.x = 12;
    38.         }
    39.        
    40.     var cloneFB : Rigidbody = Instantiate (football, transform.position,transform.rotation);
    41.     cloneFB.velocity = transform.TransformDirection(Vector3(0,speed2,speed));
    42.     chargeTime = -1.0;
    43.   }
    44. }

    Basically, the problem I am having is trying to solve is to end a catch streak if the player misses the receiver and the ball hits ground plane. But what I have tried doesn’t work or brings about new problems.

    I know this was very long and probably confusing but any help would be appreciated. I am still using Unity 1.6.2

    Thanks
     
  2. Bampf

    Bampf

    Joined:
    Oct 21, 2005
    Posts:
    369
    Isn't that what you wanted?

    If you have some additional conditions then you just have to detect them. For example, maybe the player getting tackled doesn't end the streak, just them missing the target. In which case, when the player throws the ball you set a variable (on the player or the ball) to "true" to indicate that the throw is in progress. When the ball collides with the ground plane, check the variable. If the ball was thrown then end the streak. Of course you'll have to clear the variable when the player gets ready to throw again.