1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| using UnityEngine;
using System.Collections;
public class PaddleScript : MonoBehaviour {
float paddleSpeed = 30f;
public GameObject ballPrefab;
GameObject attachedBall = null;
int lives = 3;
GUIText guiLives;
int score = 0;
public GUISkin scoreboardSkin;
// Use this for initialization
void Start () {
DontDestroyOnLoad(gameObject);
DontDestroyOnLoad(GameObject.Find("guiLives"));
guiLives = GameObject.Find("guiLives").GetComponent<GUIText>();
guiLives.text = "Lives: " + lives;
SpawnBall();
}
public void OnLevelWasLoaded( int level ) {
SpawnBall();
}
public void AddPoint(int v) {
score += v;
}
public void LoseLife() {
lives--;
guiLives.text = "Lives: " + lives;
if ( lives > 0 )
SpawnBall();
else {
Destroy(gameObject);
Application.LoadLevel("gameOver");
}
}
public void SpawnBall() {
// Spawn/Instantiate new ball
if( ballPrefab == null ) {
Debug.Log ("Hey, dummy, you forgot to link to the ball prefab in the inspector!");
return;
}
attachedBall = (GameObject)Instantiate( ballPrefab, transform.position + new Vector3(0, .75f, 0), Quaternion.identity );
}
void OnGUI() {
GUI.skin = scoreboardSkin;
GUI.Label( new Rect(0,10,300,100), "Score: " + score);
}
// Update is called once per frame
void Update () {
// Left-Right
transform.Translate( paddleSpeed * Time.deltaTime * Input.GetAxis( "Horizontal" ), 0, 0 );
if ( transform.position.x > 7.4f ) {
transform.position = new Vector3( 7.4f, transform.position.y, transform.position.z );
}
if ( transform.position.x < -7.4f ) {
transform.position = new Vector3( -7.4f, transform.position.y, transform.position.z );
}
if( attachedBall ) {
Rigidbody ballRigidbody = attachedBall.rigidbody;
ballRigidbody.position = transform.position + new Vector3(0, .45f, 0);
if( Input.GetButtonDown( "LaunchBall" ) ) {
// Fire the ball
ballRigidbody.isKinematic = false;
ballRigidbody.AddForce(300f * Input.GetAxis( "Horizontal" ), 300f, 0);
attachedBall = null;
}
}
}
void FixedUpdate() {
}
void LateUpdate() {
}
void OnCollisionEnter( Collision col ) {
foreach (ContactPoint contact in col.contacts) {
if( contact.thisCollider == collider ) {
// This is the paddle's contact point
float english = contact.point.x - transform.position.x;
contact.otherCollider.rigidbody.AddForce( 300f * english, 0, 0);
}
}
}
} |
Partager