Advertisement
summitgames

Body trigger script for ragdoll

Aug 16th, 2015
1,383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.90 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class BodyTrigger : MonoBehaviour {
  5.     public static bool finish = false;
  6.    
  7.     //used to play sounds
  8.     public AudioClip bonesCrackSound;
  9.     public AudioClip hitSound;
  10.     public AudioClip oohCrowdSound;
  11.    
  12.     private AudioSource bonesCrackSC;
  13.     private AudioSource hitSC;
  14.     private AudioSource oohCrowdSC;
  15.    
  16.     //used to show text when entered in finish
  17.     public GUIText winText;
  18.     public GUIText crashText;      
  19.     public Color winTextColor;
  20.     public Color crashTextColor;
  21.    
  22.     //used to know if next level exists.
  23.     private bool nextLevel = false;    
  24.        
  25.    
  26.     void Start()
  27.     {          
  28.         finish = false;
  29.        
  30.         //change text colors
  31.         winText.material.color = winTextColor;
  32.         crashText.material.color = crashTextColor;
  33.        
  34.         //ignoring collision between biker's bodytrigger and motorcycle body
  35.         Physics.IgnoreCollision (this.GetComponent<Collider>(), transform.parent.GetComponent<Collider>());
  36.        
  37.         //add new audio sources and add audio clips to them, used to play sounds
  38.         bonesCrackSC = gameObject.AddComponent<AudioSource>();
  39.         hitSC = gameObject.AddComponent<AudioSource>();
  40.         oohCrowdSC = gameObject.AddComponent<AudioSource>();
  41.        
  42.         bonesCrackSC.playOnAwake = false;
  43.         hitSC.playOnAwake = false;
  44.         oohCrowdSC.playOnAwake = false;
  45.        
  46.         bonesCrackSC.rolloffMode = AudioRolloffMode.Linear;
  47.         hitSC.rolloffMode = AudioRolloffMode.Linear;
  48.         oohCrowdSC.rolloffMode = AudioRolloffMode.Linear;
  49.        
  50.         bonesCrackSC.clip = bonesCrackSound;
  51.         hitSC.clip = hitSound;
  52.         oohCrowdSC.clip = oohCrowdSound;
  53.         //--------------------------------------------------
  54.     }
  55.    
  56.     void OnTriggerEnter(Collider obj)
  57.     {
  58.         if(obj.gameObject.tag == "Finish" && !Motorcycle_Controller.crash)//if entered in finish trigger
  59.         {
  60.             finish = true;
  61.            
  62.             Motorcycle_Controller.isControllable = false; //disable motorcycle controlling
  63.            
  64.             var m = transform.root.GetComponent<Motorcycle_Controller>();
  65.             m.rearWheel.GetComponent<Rigidbody>().freezeRotation = true; //disable rear wheel rotation
  66.            
  67.             winText.enabled = true; //show win text            
  68.            
  69.             if(Application.loadedLevel < Application.levelCount - 1) //if won level isn't last level (levels are set in File -> Build Settings)
  70.             {
  71.                 nextLevel = true;
  72.                
  73.                 if(m.forMobile)
  74.                     winText.text = "CONGRATULATIONS, YOU WON! \n YOUR SCORE IS: " + Motorcycle_Controller.score + "\n\n TAP ON SCREEN FOR NEXT LEVEL";
  75.                 else
  76.                     winText.text = "CONGRATULATIONS, YOU WON! \n YOUR SCORE IS: " + Motorcycle_Controller.score + "\n\n PRESS SPACE FOR NEXT LEVEL";               
  77.             }
  78.             else //won level is last one
  79.             {
  80.                 if(m.forMobile)
  81.                     winText.text = "CONGRATULATIONS, YOU WON! \n YOUR SCORE IS: " + Motorcycle_Controller.score + "\n\n TAP ON SCREEN TO PLAY FIRST LEVEL";            
  82.                 else
  83.                     winText.text = "CONGRATULATIONS, YOU WON! \n YOUR SCORE IS: " + Motorcycle_Controller.score + "\n\n PRESS SPACE TO PLAY FIRST LEVEL";              
  84.                
  85.                 nextLevel = false;
  86.             }
  87.         }
  88.         else //if entered in any other trigger than "Finish", that means player crashed
  89.         {
  90.             if(!Motorcycle_Controller.crash)
  91.             {
  92.                 Motorcycle_Controller.crash = true;
  93.                
  94.                 //play sounds
  95.                 bonesCrackSC.Play ();
  96.                 hitSC.Play ();
  97.                 oohCrowdSC.Play ();
  98.                
  99.                 if(!finish) //if we haven't entered in finish make crash text visible
  100.                 {
  101.                     crashText.enabled = true;
  102.                    
  103.                     var m = transform.root.GetComponent<Motorcycle_Controller>();
  104.                     if(m.forMobile)
  105.                         crashText.text = "TAP ON SCREEN TO RESTART";
  106.                     else
  107.                         crashText.text = "PRESS 'R' TO RESTART";
  108.                    
  109.                 }
  110.             }
  111.         }
  112.     }
  113.    
  114.     void Update()
  115.     {      
  116.         if((Input.GetKeyDown(KeyCode.Space) || (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began)) && finish) //if motorcycle entered in finish and space is pressed
  117.         {
  118.             if(nextLevel)
  119.                 Application.LoadLevel(Application.loadedLevel + 1); //load next level
  120.             else           
  121.                 Application.LoadLevel(0); //load first level
  122.         }              
  123.     }      
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement