Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2015
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.44 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class BallScript : MonoBehaviour {
  5.  
  6.         private bool ballIsActive;
  7.         private Vector3 ballPosition;
  8.         private Vector2 ballInitialForce;
  9.        
  10.         // GameObject
  11.         public GameObject playerObject;
  12.        
  13.         // Use this for initialization
  14.         void Start () {
  15.             // create the force
  16.             ballInitialForce = new Vector2 (100.0f,300.0f);
  17.            
  18.             // set to inactive
  19.             ballIsActive = false;
  20.            
  21.             // ball position
  22.             ballPosition = transform.position;
  23.         }
  24.        
  25.         // Update is called once per frame
  26.         void Update () {
  27.             // check for user input
  28.             if (Input.GetButtonDown ("Jump") == true) {
  29.                 // check if is the first play
  30.                 if (!ballIsActive){
  31.                     // reset the force
  32.                     GetComponent<Rigidbody2D>().isKinematic = false;
  33.                    
  34.                     // add a force
  35.                     GetComponent<Rigidbody2D>().AddForce(ballInitialForce);
  36.                    
  37.                     // set ball active
  38.                     ballIsActive = !ballIsActive;
  39.                 }
  40.             }
  41.            
  42.             if (!ballIsActive && playerObject != null){
  43.                
  44.                 // get and use the player position
  45.                 ballPosition.x = playerObject.transform.position.x;
  46.                
  47.                 // apply player X position to the ball
  48.                 transform.position = ballPosition;
  49.             }
  50.            
  51.             // Check if ball falls
  52.             if (ballIsActive && transform.position.y < -6) {
  53.                 ballIsActive = !ballIsActive;
  54.                 ballPosition.x = playerObject.transform.position.x;
  55.                 ballPosition.y = -4.2f;
  56.                 transform.position = ballPosition;
  57.                
  58.                 GetComponent<Rigidbody2D>().isKinematic = true;
  59.             }
  60.            
  61.         }
  62.     }
  63.  
  64.  
  65. sing UnityEngine;
  66. using System.Collections;
  67.  
  68. public class PlayerScript : MonoBehaviour {
  69.  
  70.     public float playerVelocity;
  71.     private Vector3 playerPosition;
  72.     public float boundary;
  73.  
  74.     // Use this for initialization
  75.     void Start () {
  76.         // get the initial position of the game object
  77.         playerPosition = gameObject.transform.position;
  78.     }
  79.    
  80.     // Update is called once per frame
  81.     void Update () {
  82.         // horizontal movement
  83.         playerPosition.x += Input.GetAxis("Horizontal") * playerVelocity;
  84.  
  85.         // leave the game
  86.         if (Input.GetKeyDown(KeyCode.Escape)){
  87.             Application.Quit();
  88.         }
  89.  
  90.         // update the game object transform
  91.         transform.position = playerPosition;
  92.  
  93.         // boundaries
  94.         if (playerPosition.x < -boundary) {
  95.                         transform.position = new Vector3 (-boundary, playerPosition.y, playerPosition.z);      
  96.                 }
  97.         if (playerPosition.x > boundary) {
  98.             transform.position = new Vector3(boundary, playerPosition.y, playerPosition.z);    
  99.         }
  100.  
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement