Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Arkanoid clone respawn help

Discussion in 'Scripting' started by Grimkeep, Aug 27, 2015.

  1. Grimkeep

    Grimkeep

    Joined:
    Aug 27, 2015
    Posts:
    13
    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:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Paddle : MonoBehaviour
    5. {
    6.  
    7.     private Ball ball;
    8.  
    9.  
    10.     void Start()
    11.     {
    12.         ball = GameObject.FindObjectOfType<Ball>();
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         MoveWithMouse();
    19.     }
    20.  
    21.     void MoveWithMouse()
    22.     {
    23.         Vector3 paddlePos = new Vector3(0.5f, transform.position.y, 0f);
    24.         float mousePosInBlocks = Input.mousePosition.x / Screen.width * 16;
    25.         paddlePos.x = Mathf.Clamp(mousePosInBlocks, 0.5f, 15.5f);
    26.         transform.position = paddlePos;
    27.     }
    28.  
    29. }
    Ball:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. public class Ball : MonoBehaviour
    6. {
    7.     Paddle paddle;
    8.     Vector3 paddleToBallVector;
    9.     bool hasStarted = false;
    10.  
    11.     // Use this for initialization
    12.     void Start()
    13.     {
    14.         paddle = GameObject.FindObjectOfType<Paddle>();
    15.         paddleToBallVector = transform.position - paddle.transform.position;
    16.  
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         if (!hasStarted)
    23.         {
    24.             transform.position = paddle.transform.position + paddleToBallVector;
    25.  
    26.             if (Input.GetMouseButtonDown(0))
    27.             {
    28.                 hasStarted = true;
    29.                 GetComponent<Rigidbody2D>().velocity = new Vector2(2f, 10f);
    30.             }
    31.         }
    32.     }
    33.  
    34.     void OnCollisionEnter2D(Collision2D col)
    35.     {
    36.         Vector2 tweak = new Vector2(Random.Range(0f, 0.2f), Random.Range(0f, 0.2f));
    37.  
    38.         if (hasStarted)
    39.         {
    40.             AudioSource audio = GetComponent<AudioSource>();
    41.             audio.Play();
    42.             GetComponent<Rigidbody2D>().velocity += tweak;
    43.         }
    44.     }
    45.  
    46. }
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    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.
     
  3. Grimkeep

    Grimkeep

    Joined:
    Aug 27, 2015
    Posts:
    13
    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?
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    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.
     
  5. Grimkeep

    Grimkeep

    Joined:
    Aug 27, 2015
    Posts:
    13
    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.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. public class Ball : MonoBehaviour
    6. {
    7.     Paddle paddle;
    8.     Vector3 paddleToBallVector;
    9.     bool hasStarted = false;
    10.  
    11.     // Use this for initialization
    12.     void Start()
    13.     {
    14.         paddle = FindObjectOfType<Paddle>();
    15.         paddleToBallVector = transform.position - paddle.transform.position;
    16.  
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         if (!hasStarted)
    23.         {
    24.             transform.position = paddle.transform.position + paddleToBallVector;
    25.  
    26.             if (Input.GetMouseButtonDown(0))
    27.             {
    28.                 hasStarted = true;
    29.                 GetComponent<Rigidbody2D>().velocity = new Vector2(2f, 10f);
    30.             }
    31.         }
    32.     }
    33.  
    34.     void OnCollisionEnter2D(Collision2D col)
    35.     {
    36.         Vector2 tweak = new Vector2(Random.Range(0f, 0.2f), Random.Range(0f, 0.2f));
    37.  
    38.         if (hasStarted)
    39.         {
    40.             AudioSource audio = GetComponent<AudioSource>();
    41.             audio.Play();
    42.             GetComponent<Rigidbody2D>().velocity += tweak;
    43.         }
    44.     }
    45.  
    46.     public void Reset()
    47.     {
    48.         transform.position = paddle.transform.position;
    49.         GetComponent<Rigidbody2D>().velocity = new Vector2(2f, 10f);
    50.     }
    51.  
    52. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LoseCollider : MonoBehaviour
    5. {
    6.     private LevelManager levelManager;
    7.     private LifeManager lifeSystem;
    8.     private Ball ball;
    9.  
    10.  
    11.  
    12.     void OnTriggerEnter2D(Collider2D trigger)
    13.     {
    14.         if (trigger.name == "Ball")
    15.         {
    16.             lifeSystem = FindObjectOfType<LifeManager>();
    17.             lifeSystem.TakeLife();
    18.             ball = FindObjectOfType<Ball>();
    19.             ball.Reset();
    20.         }
    21.     }
    22.  
    23. }
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    need to reset the hasStarted boolean as well as the position
     
  7. Grimkeep

    Grimkeep

    Joined:
    Aug 27, 2015
    Posts:
    13
    Im sorry to ask, but could you please show exactly how to do that. I can't seem to get it right.
     
  8. Grimkeep

    Grimkeep

    Joined:
    Aug 27, 2015
    Posts:
    13
    I still need help with this, if someone could post the exact code to achieve this, that would be really helpful.
     
  9. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. hasStarted = false;
    3.  
    o_O
     
  10. Grimkeep

    Grimkeep

    Joined:
    Aug 27, 2015
    Posts:
    13
    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.