Advertisement
Guest User

BallHandler.cs

a guest
Dec 29th, 2013
4,966
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.46 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class BallHandler : MonoBehaviour {
  5.  
  6.     // Use this for initialization
  7.     void Start () {
  8.         //this gives the ball a kick when it's spawned
  9.         rigidbody.AddForce(Vector3.right * 200 + Vector3.forward * 100);
  10.     }
  11.     // Update is called once per frame
  12.     void Update () {
  13.         //this if statement accelerates the ball until it hits a set speed
  14.         //the acceleration rate was determined experimentally to provide a challenging speedup
  15.         if (Mathf.Abs (rigidbody.velocity.x) < 40){
  16.             Vector3 shrt = rigidbody.velocity;
  17.             rigidbody.velocity = new Vector3(shrt.x + 0.001f/Time.deltaTime * Mathf.Sign(shrt.x), shrt.y, shrt.z);
  18.         }
  19.         //this locks the ball's y-axis and prevents it from rising or falling
  20.         //this line is provided for instructional purposes: a better way of doing this is to place
  21.         //a restriction on the object's rigidbody
  22.         transform.position = new Vector3(transform.position.x, 0, transform.position.z);
  23.    
  24.     }
  25.     //this is called when the object collides with something
  26.     void OnCollisionEnter(Collision other){
  27.         //this if statement gives the ball a kick in the z direction when it hits something
  28.         //this avoids the ball getting stuck in repetitive back-and-forth patterns
  29.         //the precise values used here were, again, determined exerimentally
  30.         if (Mathf.Abs (rigidbody.velocity.z) < 15){
  31.             Vector3 shrt = rigidbody.velocity;
  32.             rigidbody.AddForce(Vector3.forward * 250 * Mathf.Sign(shrt.z));
  33.         }
  34.     }      
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement