C# Version of Brackeys Tutorial

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.

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

using UnityEngine;
using System.Collections;

public class GameSetup : MonoBehaviour {

	public Camera mainCamera;
	public BoxCollider2D topWall;
	public BoxCollider2D bottomWall;
	public BoxCollider2D leftWall;
	public BoxCollider2D rightWall;

	public float edgeDistancePlayer = 75f;  //75 Pixels

	public Transform PlayerOne;
	public Transform PlayerTwo;

	void Start () {
	
		topWall.size = new Vector2 (mainCamera.ScreenToWorldPoint (new Vector3 (Screen.width * 2f, 0, 0)).x ,1f);
		topWall.center = new Vector2 (0f, mainCamera.ScreenToWorldPoint (new Vector3 (0, Screen.height, 0)).y + 0.5f);

		bottomWall.size = new Vector2 (mainCamera.ScreenToWorldPoint (new Vector3 (Screen.width * 2, 0f, 0f)).x, 1f);
		bottomWall.center = new Vector2 (0f, mainCamera.ScreenToWorldPoint (new Vector3( 0f, 0f, 0f)).y - 0.5f);
		
		leftWall.size = new Vector2(1f, mainCamera.ScreenToWorldPoint(new Vector3(0f, Screen.height*2f, 0f)).y);;
		leftWall.center = new Vector2(mainCamera.ScreenToWorldPoint(new Vector3(0f, 0f, 0f)).x - 0.5f, 0f);
		
		rightWall.size = new Vector2(1f, mainCamera.ScreenToWorldPoint(new Vector3(0f, Screen.height*2f, 0f)).y);
		rightWall.center = new Vector2(mainCamera.ScreenToWorldPoint(new Vector3(Screen.width, 0f, 0f)).x + 0.5f, 0f);

		PlayerOne.position = new Vector3(mainCamera.ScreenToWorldPoint (new Vector3 (edgeDistancePlayer, 0f, 0f)).x, 0f, 0f);
		PlayerTwo.position = new Vector3(mainCamera.ScreenToWorldPoint (new Vector3 (Screen.width - edgeDistancePlayer, 0f, 0f)).x, 0f, 0f);

	}
}

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

And the other Script:

using UnityEngine;
using System.Collections;

public class PlayerControls : MonoBehaviour {

	public KeyCode moveUp = KeyCode.W;
	public KeyCode moveDown = KeyCode.S;

	public float speed = 10;
	



	void Update () {
	
			if(Input.GetKey(moveUp))
		   	{
				rigidbody2D.velocity = new Vector3(0, speed , 0);
			}

			else if(Input.GetKey(moveDown))
		    {
				rigidbody2D.velocity = new Vector3(0, -1 * speed , 0);
			}
			else
			{
				rigidbody2D.velocity = new Vector3(0, 0 , 0);
			}
	}
}

1479812–81869–$GameSetup.cs (1.49 KB)
1479812–81870–$PlayerControls.cs (498 Bytes)

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.

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

using UnityEngine;
using System.Collections;



public class BallControl : MonoBehaviour {


	public int randomNumber;
	public float velY;
	public float velX;




	void Start () {

		randomNumber = Random.Range(2, 3);
		if(randomNumber == 2)
		{
			rigidbody2D.AddForce (new Vector2 (80, 10));
		}
		else
		{
			rigidbody2D.AddForce (new Vector2 (-80, -10));
		}

	}
	

	void OnCollisionEnter2D (Collision2D colInfo) {
	
		if (colInfo.collider.tag == "Player")
		{
			velY = rigidbody2D.velocity.y;
			velX = rigidbody2D.velocity.x;
			rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x , velY/2 + colInfo.collider.rigidbody2D.velocity.y/3);
		}



	}
}

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

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

1479923–81892–$BallControl.cs (664 Bytes)

Tried to PM you but cant

Hello.

You might want to put your posted code in the proper code tags:

http://forum.unity3d.com/threads/143875-Using-code-tags-properly

Thanks for posting code.

Thanks for letting me know :slight_smile:

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.

using UnityEngine;
using System.Collections;

public class SideWalls : MonoBehaviour {




	void OnTriggerEnter2D (Collider2D hitInfo) {
		if(hitInfo.name == "Ball")
		{
			string wallName = transform.name;
			GameManager.Score (wallName);
		}

	}
}
using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour {

	public static int playerScore01 = 0;  // probably no need for "public" here
	public static int playerScore02 = 0;

	public GUISkin theSkin;



	

	public static void Score (string wallName) {  //the "public tag" is important, otherwise you cant acces it form "SideWalls.cs" 
		if(wallName == "_rightWall")
		{
			playerScore01 += 1;
		}

		else 
		{
			playerScore02 += 1;
		}
		Debug.Log ("Player Score 01 is" + playerScore01);
		Debug.Log ("Player Score 02 is" + playerScore02);



	
	}


	void OnGUI() {

		GUI.skin = theSkin;
		GUI.Label (new Rect (Screen.width / 2 - 150, 25, 100, 100), "" + playerScore01);
		GUI.Label (new Rect (Screen.width / 2 + 150, 25, 100, 100), "" + playerScore02);


	}

}

1489821–83095–$GameManager.cs (797 Bytes)
1489821–83096–$SideWalls.cs (253 Bytes)

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.

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

using UnityEngine;
using System.Collections;



public class BallControl : MonoBehaviour {


	public int randomNumber;
	public float velY;
	public float velX;

	float ballSpeed = 100;


	void Start () {

		StartCoroutine(YieldFunctionOne());   //in C# you have to put a yield WaitForSeconds in an IEnumerator! Also call the Function with: StartCoroutine(name());

	}
	

	void OnCollisionEnter2D (Collision2D colInfo) {
	
		if (colInfo.collider.tag == "Player")
		{
			velY = rigidbody2D.velocity.y;
			velX = rigidbody2D.velocity.x;
			rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x , velY/2 + colInfo.collider.rigidbody2D.velocity.y/3);
		}



	}

	void GoBall() {

		randomNumber = Random.Range(2, 10);
		if(randomNumber <= 5)
		{
			rigidbody2D.AddForce (new Vector2 (ballSpeed, 10));
		}
		else
		{
			rigidbody2D.AddForce (new Vector2 (-1*ballSpeed, -10));
		}

	}

	void ResetBall(){

		rigidbody2D.velocity = new Vector3(0, 0, 0);
		transform.position = new Vector3(0,0,0);
		StartCoroutine(YieldFunctionTwo());

	}

	IEnumerator YieldFunctionOne() {    //you could also put the two functions together!

		yield return new WaitForSeconds(2);
		GoBall();

	}

	IEnumerator YieldFunctionTwo() {
		
		yield return new WaitForSeconds(0.5f);
		GoBall();
		
	}
}

The rest is just like in Java :slight_smile:

1516268–86410–$BallControl.cs (1.25 KB)

Thanks a lot!! =)