- Home /
Unity's Breakout Tutorial Ball Issue.
I went through the Unity Tutorial for Creating a Breakout Game. I have two issues. First is that when the ball passes through the Death Zone at the bottom of the play field it never gets destroyed. I've tried a few things but always end up with two balls falling through empty space by the time I get to life three. And the second one is that the Ball sometime drifts enough on the Z axis to fall out of the screen. (Most likely due to the custom oval shaped paddle I have created to make it easier to pop the ball up into the play field when it starts bouncing almost perfectly Horizontal.) the temporary solution I have created is to create two solid cubes that are transparent to keep the ball true. But occasionally it still differs by .019 on the Z and it causes the ball to become extremely slow and sometime stop eventually when it hits the paddle and wall at the same time. I'm using the standard programming for the game that is in the unity Tutorial. I have checked it multiple times and can't find anything that I may have missed. I'm sure that I caused the problem with the new paddle and my Improvised restraints. IS there and easy fix for these?
We would need to see some code. Your wall of text is a little tricky to get anything useful from :)
I do over explain a bit sorry. Here is the Ball section
using UnityEngine;
using System.Collections;
public class Ball : $$anonymous$$onoBehaviour {
public float ballInitialVelocity = 600.0f;
private Rigidbody rb;
private bool ballInPlay;
// Use this for initialization
void Awake () {
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update ()
{
if (Input.GetButtonDown("Fire1") && ballInPlay == false)
{
transform.parent = null;
ballInPlay = true;
rb.is$$anonymous$$inematic = false;
rb.AddForce(new Vector3(ballInitialVelocity, ballInitialVelocity, 0));
}
}
}
The G$$anonymous$$.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class G$$anonymous$$ : $$anonymous$$onoBehaviour
{
public int lives = 3;
public int bricks = 20;
public int score = 0;
public float resetDelay = 1.0f;
public Text livesText;
public Text scoreText;
public GameObject gameOver;
public GameObject youWon;
public GameObject bricksPrefab;
public GameObject paddle;
public GameObject deathParticles;
public static G$$anonymous$$ instance = null;
private GameObject clonePaddle;
// Use this for initialization
void Awake ()
{
if (instance == null)
instance = this;
else if (instance != null)
Destroy (gameObject);
Setup ();
}
public void Setup ()
{
clonePaddle = Instantiate (paddle, transform.position, Quaternion.identity) as GameObject;
Instantiate (bricksPrefab, transform.position, Quaternion.identity);
}
void CheckGameOver ()
{
if (bricks < 1) {
youWon.SetActive (true);
Time.timeScale = 0.25f;
Invoke ("Reset", resetDelay);
}
if (lives < 1) {
gameOver.SetActive (true);
Time.timeScale = 0.25f;
Invoke ("Reset", resetDelay);
}
}
void Reset ()
{
Time.timeScale = 1.0f;
Application.LoadLevel (Application.loadedLevel);
}
public void LoseLife ()
{
lives--;
livesText.text = "Lives: " + lives;
Instantiate (deathParticles, clonePaddle.transform.position, Quaternion.identity);
Destroy (clonePaddle);
Invoke ("SetupPaddle", resetDelay);
CheckGameOver ();
}
void SetupPaddle ()
{
clonePaddle = Instantiate (paddle, transform.position, Quaternion.identity) as GameObject;
}
public void DestroyBrick ()
{
bricks--;
score = score + 100;
scoreText.text = "Score: " + score;
CheckGameOver ();
}
}
G$$anonymous$$ needs reformatting :P
Highlight all the code and click the 101010 button.
And the DeathZone
using UnityEngine;
using System.Collections;
public class DeathZone : $$anonymous$$onoBehaviour {
void OnTriggerEnter(Collider col)
{
G$$anonymous$$.instance.LoseLife();
}
}
Answer by DadPool · Jan 31, 2015 at 07:03 AM
Well I fixed my own Issues. To lock the position of the Z travel for the ball. I just went into the inspector and check marked the Z constraint for the position. As far as the code I added a bit to the Ball code here is the new code.
using UnityEngine;
using System.Collections;
public class Ball : MonoBehaviour
{
public float ballInitialVelocity = 600.0f;
public GameObject floor;
private Rigidbody rb;
private bool ballInPlay;
// Use this for initialization
void Awake ()
{
rb = GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update ()
{
if (Input.GetButtonDown ("Fire1") && ballInPlay == false) {
transform.parent = null;
ballInPlay = true;
rb.isKinematic = false;
rb.AddForce (new Vector3 (ballInitialVelocity, ballInitialVelocity, 0));
}
}
void OnTriggerEnter (Collider floor)
{
Destroy (gameObject);
}
}
Now I don't have extra balls floating off into eternity while gameplay is going. I was also able to delete the two invisible barriers to lock in the ball to the play area.
Your answer
Follow this Question
Related Questions
FIXED: How can I stop a collider from tunneling? 2 Answers
Unity3D: Get Velocity after collision 0 Answers
Show trajectory path a ball will take based on collision 1 Answer
Optimize hundreds of objects 2 Answers
Manually check collision ? 1 Answer