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)