Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class BallHandler : MonoBehaviour {
- // Use this for initialization
- void Start () {
- //this gives the ball a kick when it's spawned
- rigidbody.AddForce(Vector3.right * 200 + Vector3.forward * 100);
- }
- // Update is called once per frame
- void Update () {
- //this if statement accelerates the ball until it hits a set speed
- //the acceleration rate was determined experimentally to provide a challenging speedup
- if (Mathf.Abs (rigidbody.velocity.x) < 40){
- Vector3 shrt = rigidbody.velocity;
- rigidbody.velocity = new Vector3(shrt.x + 0.001f/Time.deltaTime * Mathf.Sign(shrt.x), shrt.y, shrt.z);
- }
- //this locks the ball's y-axis and prevents it from rising or falling
- //this line is provided for instructional purposes: a better way of doing this is to place
- //a restriction on the object's rigidbody
- transform.position = new Vector3(transform.position.x, 0, transform.position.z);
- }
- //this is called when the object collides with something
- void OnCollisionEnter(Collision other){
- //this if statement gives the ball a kick in the z direction when it hits something
- //this avoids the ball getting stuck in repetitive back-and-forth patterns
- //the precise values used here were, again, determined exerimentally
- if (Mathf.Abs (rigidbody.velocity.z) < 15){
- Vector3 shrt = rigidbody.velocity;
- rigidbody.AddForce(Vector3.forward * 250 * Mathf.Sign(shrt.z));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement