Search Unity

C# Version of Brackeys Tutorial

Discussion in 'Community Learning & Teaching' started by SC4V4NGER, Jan 10, 2014.

  1. SC4V4NGER

    SC4V4NGER

    Joined:
    Dec 15, 2013
    Posts:
    5
    This is the c# Version for a Java Script made by Brackeys in his simple Tutorial "3. How to make a 2D Game - Unity 4.3 Tutorial " (Part 3)

    (https://www.youtube.com/user/Brackeys)
    (https://www.youtube.com/watch?v=rlLMwNI53Oo)
    (http://brackeys.com)

    Full Credit goes to him.

    ///////////////////////////////////////////////////

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameSetup : MonoBehaviour {
    5.  
    6.     public Camera mainCamera;
    7.     public BoxCollider2D topWall;
    8.     public BoxCollider2D bottomWall;
    9.     public BoxCollider2D leftWall;
    10.     public BoxCollider2D rightWall;
    11.  
    12.     public float edgeDistancePlayer = 75f;  //75 Pixels
    13.  
    14.     public Transform PlayerOne;
    15.     public Transform PlayerTwo;
    16.  
    17.     void Start () {
    18.    
    19.         topWall.size = new Vector2 (mainCamera.ScreenToWorldPoint (new Vector3 (Screen.width * 2f, 0, 0)).x ,1f);
    20.         topWall.center = new Vector2 (0f, mainCamera.ScreenToWorldPoint (new Vector3 (0, Screen.height, 0)).y + 0.5f);
    21.  
    22.         bottomWall.size = new Vector2 (mainCamera.ScreenToWorldPoint (new Vector3 (Screen.width * 2, 0f, 0f)).x, 1f);
    23.         bottomWall.center = new Vector2 (0f, mainCamera.ScreenToWorldPoint (new Vector3( 0f, 0f, 0f)).y - 0.5f);
    24.        
    25.         leftWall.size = new Vector2(1f, mainCamera.ScreenToWorldPoint(new Vector3(0f, Screen.height*2f, 0f)).y);;
    26.         leftWall.center = new Vector2(mainCamera.ScreenToWorldPoint(new Vector3(0f, 0f, 0f)).x - 0.5f, 0f);
    27.        
    28.         rightWall.size = new Vector2(1f, mainCamera.ScreenToWorldPoint(new Vector3(0f, Screen.height*2f, 0f)).y);
    29.         rightWall.center = new Vector2(mainCamera.ScreenToWorldPoint(new Vector3(Screen.width, 0f, 0f)).x + 0.5f, 0f);
    30.  
    31.         PlayerOne.position = new Vector3(mainCamera.ScreenToWorldPoint (new Vector3 (edgeDistancePlayer, 0f, 0f)).x, 0f, 0f);
    32.         PlayerTwo.position = new Vector3(mainCamera.ScreenToWorldPoint (new Vector3 (Screen.width - edgeDistancePlayer, 0f, 0f)).x, 0f, 0f);
    33.  
    34.     }
    35. }
    36.  
    /////////////////////////////////////////////////

    And the other Script:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerControls : MonoBehaviour {
    5.  
    6.     public KeyCode moveUp = KeyCode.W;
    7.     public KeyCode moveDown = KeyCode.S;
    8.  
    9.     public float speed = 10;
    10.    
    11.  
    12.  
    13.  
    14.     void Update () {
    15.    
    16.             if(Input.GetKey(moveUp))
    17.             {
    18.                 rigidbody2D.velocity = new Vector3(0, speed , 0);
    19.             }
    20.  
    21.             else if(Input.GetKey(moveDown))
    22.             {
    23.                 rigidbody2D.velocity = new Vector3(0, -1 * speed , 0);
    24.             }
    25.             else
    26.             {
    27.                 rigidbody2D.velocity = new Vector3(0, 0 , 0);
    28.             }
    29.     }
    30. }
     

    Attached Files:

    Last edited: Jan 11, 2014
  2. SC4V4NGER

    SC4V4NGER

    Joined:
    Dec 15, 2013
    Posts:
    5
    This is the c# Version for a Java Script made by Brackeys in his simple Tutorial "4. How to make a 2D Game - Unity 4.3 Tutorial " (Part 4)

    (https://www.youtube.com/user/Brackeys)
    (https://www.youtube.com/watch?v=_jwicKUEnhg)
    (http://brackeys.com)

    Full Credit goes to him.

    ///////////////////////////////////////////////////
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5.  
    6. public class BallControl : MonoBehaviour {
    7.  
    8.  
    9.     public int randomNumber;
    10.     public float velY;
    11.     public float velX;
    12.  
    13.  
    14.  
    15.  
    16.     void Start () {
    17.  
    18.         randomNumber = Random.Range(2, 3);
    19.         if(randomNumber == 2)
    20.         {
    21.             rigidbody2D.AddForce (new Vector2 (80, 10));
    22.         }
    23.         else
    24.         {
    25.             rigidbody2D.AddForce (new Vector2 (-80, -10));
    26.         }
    27.  
    28.     }
    29.    
    30.  
    31.     void OnCollisionEnter2D (Collision2D colInfo) {
    32.    
    33.         if (colInfo.collider.tag == "Player")
    34.         {
    35.             velY = rigidbody2D.velocity.y;
    36.             velX = rigidbody2D.velocity.x;
    37.             rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x , velY/2 + colInfo.collider.rigidbody2D.velocity.y/3);
    38.         }
    39.  
    40.  
    41.  
    42.     }
    43. }
    44.  
    /////////////////////

    I did not change PlayerControls.cs, just change the mass for the actual Onject in your Hierarchy :)
     

    Attached Files:

    Last edited: Jan 11, 2014
  3. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
  4. SC4V4NGER

    SC4V4NGER

    Joined:
    Dec 15, 2013
    Posts:
    5
    Thanks for letting me know :)
     
  5. SC4V4NGER

    SC4V4NGER

    Joined:
    Dec 15, 2013
    Posts:
    5
    This is the c# Version for a Java Script made by Brackeys in his simple Tutorial "5. How to make a 2D Game - Unity 4.3 Tutorial " (Part 5)

    (https://www.youtube.com/user/Brackeys)
    (https://www.youtube.com/watch?v=WztRaOtNQbI)
    (http://brackeys.com)

    Full Credit goes to him.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SideWalls : MonoBehaviour {
    5.  
    6.  
    7.  
    8.  
    9.     void OnTriggerEnter2D (Collider2D hitInfo) {
    10.         if(hitInfo.name == "Ball")
    11.         {
    12.             string wallName = transform.name;
    13.             GameManager.Score (wallName);
    14.         }
    15.  
    16.     }
    17. }
    18.  
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameManager : MonoBehaviour {
    5.  
    6.     public static int playerScore01 = 0;  // probably no need for "public" here
    7.     public static int playerScore02 = 0;
    8.  
    9.     public GUISkin theSkin;
    10.  
    11.  
    12.  
    13.    
    14.  
    15.     public static void Score (string wallName) {  //the "public tag" is important, otherwise you cant acces it form "SideWalls.cs"
    16.         if(wallName == "_rightWall")
    17.         {
    18.             playerScore01 += 1;
    19.         }
    20.  
    21.         else
    22.         {
    23.             playerScore02 += 1;
    24.         }
    25.         Debug.Log ("Player Score 01 is" + playerScore01);
    26.         Debug.Log ("Player Score 02 is" + playerScore02);
    27.  
    28.  
    29.  
    30.    
    31.     }
    32.  
    33.  
    34.     void OnGUI() {
    35.  
    36.         GUI.skin = theSkin;
    37.         GUI.Label (new Rect (Screen.width / 2 - 150, 25, 100, 100), "" + playerScore01);
    38.         GUI.Label (new Rect (Screen.width / 2 + 150, 25, 100, 100), "" + playerScore02);
    39.  
    40.  
    41.     }
    42.  
    43. }
     

    Attached Files:

  6. SC4V4NGER

    SC4V4NGER

    Joined:
    Dec 15, 2013
    Posts:
    5
    This is the c# Version for a Java Script made by Brackeys in his simple Tutorial "6. How to make a 2D Game - Unity 4.3 Tutorial " (Part 6)

    (https://www.youtube.com/user/Brackeys)
    (https://www.youtube.com/watch?v=yQ1NLb59k3U)
    (http://brackeys.com)

    Full Credit goes to him.

    ///////////////////////////////////////////////////

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5.  
    6. public class BallControl : MonoBehaviour {
    7.  
    8.  
    9.     public int randomNumber;
    10.     public float velY;
    11.     public float velX;
    12.  
    13.     float ballSpeed = 100;
    14.  
    15.  
    16.     void Start () {
    17.  
    18.         StartCoroutine(YieldFunctionOne());   //in C# you have to put a yield WaitForSeconds in an IEnumerator! Also call the Function with: StartCoroutine(name());
    19.  
    20.     }
    21.    
    22.  
    23.     void OnCollisionEnter2D (Collision2D colInfo) {
    24.    
    25.         if (colInfo.collider.tag == "Player")
    26.         {
    27.             velY = rigidbody2D.velocity.y;
    28.             velX = rigidbody2D.velocity.x;
    29.             rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x , velY/2 + colInfo.collider.rigidbody2D.velocity.y/3);
    30.         }
    31.  
    32.  
    33.  
    34.     }
    35.  
    36.     void GoBall() {
    37.  
    38.         randomNumber = Random.Range(2, 10);
    39.         if(randomNumber <= 5)
    40.         {
    41.             rigidbody2D.AddForce (new Vector2 (ballSpeed, 10));
    42.         }
    43.         else
    44.         {
    45.             rigidbody2D.AddForce (new Vector2 (-1*ballSpeed, -10));
    46.         }
    47.  
    48.     }
    49.  
    50.     void ResetBall(){
    51.  
    52.         rigidbody2D.velocity = new Vector3(0, 0, 0);
    53.         transform.position = new Vector3(0,0,0);
    54.         StartCoroutine(YieldFunctionTwo());
    55.  
    56.     }
    57.  
    58.     IEnumerator YieldFunctionOne() {    //you could also put the two functions together!
    59.  
    60.         yield return new WaitForSeconds(2);
    61.         GoBall();
    62.  
    63.     }
    64.  
    65.     IEnumerator YieldFunctionTwo() {
    66.        
    67.         yield return new WaitForSeconds(0.5f);
    68.         GoBall();
    69.        
    70.     }
    71. }

    The rest is just like in Java :)
     

    Attached Files:

  7. lauchi

    lauchi

    Joined:
    Sep 15, 2014
    Posts:
    1
    Thanks a lot!! =)