HI, so im working on this arkanoid clone and im having some problems on how to get the ball to respawn. Right now when the game starts the ball is sitting on the paddle and I can move left and right. When the mouse is clicked the ball launches. In the current state when the ball is missed it hits a collider below the paddle and calling a gameover scene. What I want to happen is when it hits the collider respawn back on the paddle and wait for a mouse click.
Paddle:
using UnityEngine;
using System.Collections;
public class Paddle : MonoBehaviour
{
private Ball ball;
void Start()
{
ball = GameObject.FindObjectOfType<Ball>();
}
// Update is called once per frame
void Update()
{
MoveWithMouse();
}
void MoveWithMouse()
{
Vector3 paddlePos = new Vector3(0.5f, transform.position.y, 0f);
float mousePosInBlocks = Input.mousePosition.x / Screen.width * 16;
paddlePos.x = Mathf.Clamp(mousePosInBlocks, 0.5f, 15.5f);
transform.position = paddlePos;
}
}
Ball:
using UnityEngine;
using System.Collections;
public class Ball : MonoBehaviour
{
Paddle paddle;
Vector3 paddleToBallVector;
bool hasStarted = false;
// Use this for initialization
void Start()
{
paddle = GameObject.FindObjectOfType<Paddle>();
paddleToBallVector = transform.position - paddle.transform.position;
}
// Update is called once per frame
void Update()
{
if (!hasStarted)
{
transform.position = paddle.transform.position + paddleToBallVector;
if (Input.GetMouseButtonDown(0))
{
hasStarted = true;
GetComponent<Rigidbody2D>().velocity = new Vector2(2f, 10f);
}
}
}
void OnCollisionEnter2D(Collision2D col)
{
Vector2 tweak = new Vector2(Random.Range(0f, 0.2f), Random.Range(0f, 0.2f));
if (hasStarted)
{
AudioSource audio = GetComponent<AudioSource>();
audio.Play();
GetComponent<Rigidbody2D>().velocity += tweak;
}
}
}
add a Reset() function to the ball script, have it set the position and hasStarted. When the ball hits the collider have it call that function on the ball.
Thanks for the reply, so I tried what you said but i get an error “Reset() is inaccessible due to its protection level”. I should mention my C# skills are minimal, so im probably just doing it wrong. Could you elaborate on how i would do it?
you’ll need “public” in front of the function in order for it to be called from another script.
In c# leaving out the access modifier means it defaults to private (only accessible from within this script). The unity methods “cheat” at this though, so don’t look too closely at Update, Start, OnCollision### etc.
I remembered to use “public” after the posting, but still having some problems. The ball aromatically launches from the paddle right after loosing a ball. What im wanting is the ball to respawn on the paddle and await my input to relaunch it.
using UnityEngine;
using System.Collections;
public class Ball : MonoBehaviour
{
Paddle paddle;
Vector3 paddleToBallVector;
bool hasStarted = false;
// Use this for initialization
void Start()
{
paddle = FindObjectOfType<Paddle>();
paddleToBallVector = transform.position - paddle.transform.position;
}
// Update is called once per frame
void Update()
{
if (!hasStarted)
{
transform.position = paddle.transform.position + paddleToBallVector;
if (Input.GetMouseButtonDown(0))
{
hasStarted = true;
GetComponent<Rigidbody2D>().velocity = new Vector2(2f, 10f);
}
}
}
void OnCollisionEnter2D(Collision2D col)
{
Vector2 tweak = new Vector2(Random.Range(0f, 0.2f), Random.Range(0f, 0.2f));
if (hasStarted)
{
AudioSource audio = GetComponent<AudioSource>();
audio.Play();
GetComponent<Rigidbody2D>().velocity += tweak;
}
}
public void Reset()
{
transform.position = paddle.transform.position;
GetComponent<Rigidbody2D>().velocity = new Vector2(2f, 10f);
}
}
using UnityEngine;
using System.Collections;
public class LoseCollider : MonoBehaviour
{
private LevelManager levelManager;
private LifeManager lifeSystem;
private Ball ball;
void OnTriggerEnter2D(Collider2D trigger)
{
if (trigger.name == "Ball")
{
lifeSystem = FindObjectOfType<LifeManager>();
lifeSystem.TakeLife();
ball = FindObjectOfType<Ball>();
ball.Reset();
}
}
}
WOW I feel stupid! I swear I tried that already and the result was the ball not spawning at all. But know it somehow works perfectly. That was a whole day wasted over something so simple, I was way over complicating the problem. Thanks for all the help.