so ive been working on a simple Breakout style game. problem is, when you lose the ball below the paddle, the ball wont reset its speed to minimum. heres the script for the ball.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Ball : MonoBehaviour {
public float multiplier = 1.1f;
public float minSpeed = 5;
public float maxSpeed = 10;
public float curSpeed = 0;
private Transform _t;
public List<GameObject> blocks;
void Awake() {
_t = transform;
blocks = new List<GameObject>();
}
// Use this for initialization
void Start() {
blocks.AddRange( GameObject.FindGameObjectsWithTag( "Block" ) );
ResetBall();
}
// Update is called once per frame
void Update() {
if( blocks.Count <1 )
WonLevel();
if( _t.position.y < -5 )
MissedBall ();
}
void FixedUpdate() {
curSpeed = Vector3.Magnitude( rigidbody.velocity );
if (curSpeed > maxSpeed )
rigidbody.velocity /= curSpeed / maxSpeed;
if ( curSpeed < minSpeed && curSpeed > 0 )
rigidbody.velocity /= curSpeed / minSpeed;
}
void OnCollisionEnter( Collision col ) {
rigidbody.velocity += rigidbody.velocity * multiplier;
if( col.gameObject.CompareTag( "Block" ) )
blocks.Remove( col.gameObject );
}
private void MissedBall() {
if( Lives.curLives-- < 2 )
GameOver();
else
ResetBall();
}
private void ResetBall() {
_t.position = new Vector3( Random.Range( -Player.xBoundry + 2 , Player.xBoundry - 2), 2, Player.zPosition );
_t.LookAt( GameObject.FindGameObjectWithTag( "Player" ).transform.position );
rigidbody.AddRelativeForce( new Vector3( 0, 0, minSpeed ) );
}
private void GameOver() {
Debug.Log(" Game Over");
_t.renderer.enabled = false;
rigidbody.velocity = Vector3.zero;
_t.position = Vector3.zero;
}
private void WonLevel() {
Debug.Log( "We Won Level" );
}
}