Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Introduction to my game / Need Help :D

Discussion in 'Getting Started' started by Gromis, Jan 21, 2015.

  1. Gromis

    Gromis

    Joined:
    Dec 15, 2014
    Posts:
    7
    Yo guys !!!!
    How's your day ? My day is really good !
    Today am on the end of finishing my first game (i'm new in unity)
    and i have few bugs which i tried to fix myself but with no succes.
    I created a video for showing all the bugs i will link the video at the end.
    So the few bugs i get the ball speed goes really fast and its impossible to try to block it,second bug is that when the ball hits the goal it doeasnt respawn and he needs to go in oposite direction,the third is that in different resolution scale main camera size changes which reveals the game underborders(what is behined borders).
    MY SCRIPTS:

    GOAL SCRIPT:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class GoalScript : MonoBehaviour {
    6.     public int attackingPlayer;
    7.     public GameManagerScript gameMan;
    8.     void OnCollisionEnter2D(Collision2D other)
    9.     {
    10.         if(other.transform.name == "Ball")
    11.         {
    12.             gameMan.GoalScored(attackingPlayer);
    13.         }
    14.     }
    15. }
    16.  
    GameManagerScript:
    Code (CSharp):
    1. using UnityEngine.UI;
    2. using System.Collections;
    3. using UnityEngine;
    4. public class GameManagerScript:MonoBehaviour{
    5.     // Use this for initialization
    6.     public int playerOneScore, playerTwoScore;
    7.     public Text scoreText;
    8.     public BallScript gameBall;
    9.  
    10.     void Start () {
    11.         playerOneScore = 0;
    12.         playerTwoScore = 0;
    13.     }
    14.     public void GoalScored(int playerNumber)
    15.     {
    16.         // increase the score for whichever player scored
    17.         if(playerNumber == 1)
    18.             playerOneScore++;
    19.  
    20.         else if (playerNumber ==2)
    21.             playerTwoScore++;
    22.        
    23.         // now check if the player has won
    24.         if (playerOneScore == 3)
    25.                         Application.LoadLevel (2);
    26.                 else if (playerTwoScore == 3)
    27.                         Application.LoadLevel (3);
    28.         UpdateScoreText();
    29.     }
    30.     public void GameOver(int winner)
    31.     {
    32.         // this is called when a player reaches 3 points
    33.        
    34.         // reset the scores
    35.         playerOneScore = 0;
    36.         playerTwoScore = 0;
    37.         gameBall.Reset ();
    38.         UpdateScoreText();
    39.     }
    40.     void UpdateScoreText()
    41.     {
    42.         scoreText.text = "Player One " + playerOneScore.ToString() + " - " + playerTwoScore.ToString() + " Player Two";
    43.     }
    44. }
    45.  
    PaddleScript:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PaddleScript : MonoBehaviour {
    5.    
    6.     [SerializeField]
    7.     bool isPlayerTwo;
    8.     [SerializeField]
    9.     float speed = 0.2f;       // how far the paddle moves per frame
    10.     Transform myTransform;    // reference to the object's transform
    11.     int direction = 0; // 0 = still, 1= up, -1 = down
    12.     float previousPositionY;
    13.     // Use this for initialization
    14.     void Start () {
    15.         myTransform = transform;
    16.         previousPositionY = myTransform.position.y;
    17.     }
    18.    
    19.     // FixedUpdate is called once per physics tick/frame
    20.     void FixedUpdate () {
    21.         // first decide if this is player 1 or player 2 so we know what keys to listen for
    22.         if (isPlayerTwo)
    23.         {
    24.             if (Input.GetKey ("w"))
    25.                 MoveUp ();
    26.             else if (Input.GetKey ("s"))
    27.                 MoveDown ();
    28.         }
    29.         else // if not player 2 it must be player 1
    30.         {
    31.             if (Input.GetKey ("o"))
    32.                 MoveUp ();
    33.             else if (Input.GetKey ("l"))
    34.                 MoveDown ();
    35.         }
    36.         if (previousPositionY > myTransform.position.y)
    37.             direction = -1;
    38.         else if (previousPositionY < myTransform.position.y)
    39.             direction =1;
    40.         else
    41.             direction = 0;
    42.     }
    43.    
    44.     // move the player's paddle up by an amount determined by 'speed'
    45.     void MoveUp()
    46.     {
    47.         myTransform.position = new Vector2(myTransform.position.x, myTransform.position.y + speed);
    48.     }
    49.    
    50.     // move the player's paddle down by an amount determined by 'speed'
    51.     void MoveDown()
    52.     {
    53.         myTransform.position = new Vector2 (myTransform.position.x, myTransform.position.y - speed);          
    54.     }
    55.     void LateUpdate()
    56.     {
    57.         previousPositionY = myTransform.position.y;
    58.     }
    59.     void OnCollisionExit2D(Collision2D other)
    60.     {
    61.         float adjust = 5 * direction;
    62.         other.rigidbody.velocity = new Vector2(other.rigidbody.velocity.x, other.rigidbody.velocity.y + adjust);      
    63.     }
    64. }

    If you need more jsut watch the video i showed it all and introduce the game how it will work :)