Search Unity

Brick Game getting error

Discussion in 'Documentation' started by zalimdaner, Jan 4, 2021.

  1. zalimdaner

    zalimdaner

    Joined:
    Sep 27, 2020
    Posts:
    21
    I am trying to maek brick breaker game i can move paddle but the problem When press play button the ball is not come down and i also getting error on my ball script

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Ballcontrol : MonoBehaviour
    6. {
    7.     public Vector2 currentVel;
    8.     public Vector2 ballvel;
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         ballvel = new Vector2(3, -3);
    14.         GetComponent<Rigidbody>().velocity = ballvel;
    15.  
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         currentVel = GetComponent<Rigidbody2D>().velocity;
    22.         if (currentVel.y > 6)
    23.         {
    24.             GetComponent<Rigidbody2D>().velocity = new Vector2(currentVel.x, 6);
    25.         }
    26.         if (currentVel.y < -6)
    27.         {
    28.             GetComponent<Rigidbody2D>().velocity = new Vector2(currentVel.x, -6);
    29.         }
    30.         if (currentVel.x > 6)
    31.         {
    32.             GetComponent<Rigidbody2D>().velocity = new Vector2(6, currentVel.y);
    33.         }
    34.  
    35.         if (currentVel.x < -6)
    36.         {
    37.             GetComponent<Rigidbody2D>().velocity = new Vector2(-6, currentVel.y);
    38.         }
    39.     }
    40.     void OnCollisionEnter2D(Collision2D Other)
    41.  
    42.     {
    43.         if (Other.gameObject.name == "Left")
    44.         {
    45.             GetComponent<Rigidbody2D>().velocity = new Vector2((currentVel.x * -1), (currentVel.y));
    46.         }
    47.         if (Other.gameObject.name == "Right")
    48.         {
    49.             GetComponent<Rigidbody2D>().velocity = new Vector2((currentVel.x * -1), (currentVel.y));
    50.         }
    51.  
    52.     }
    53. }
    54.