Reputation: 629
I've created a simple drag script. This will be the main input of my game and that's how I'll control the player. The script can be seen below:
public void OnMouseDown() {
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
if (gameController.GetCurrentState () != GameStates.INGAME){
gameController.StartGame();
}
}
public void OnMouseDrag() {
if (gameController.GetCurrentState () == GameStates.INGAME) {
Vector3 curScreenPoint = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint (curScreenPoint) + offset;
transform.position = curPosition;
}
}
This script is attached to my player. Also attached to my player is a RigidBody 2D and Circle Collider 2D. I've created some walls and at game start, I'm repositioning it outside of the camera using mainCam.ScreenToWorldPoint. This is done with:
var mainCam : Camera;
var topWall : BoxCollider2D;
var bottomWall : BoxCollider2D;
var leftWall : BoxCollider2D;
var rightWall : BoxCollider2D;
//Reference the players
function Start () {
//Move each wall to its edge location:
topWall.size = new Vector2 (mainCam.ScreenToWorldPoint (new Vector3 (Screen.width * 2f, 0f, 0f)).x, 1f);
topWall.center = new Vector2 (0f, mainCam.ScreenToWorldPoint (new Vector3 ( 0f, Screen.height, 0f)).y + 0.5f);
bottomWall.size = new Vector2 (mainCam.ScreenToWorldPoint (new Vector3 (Screen.width * 2, 0f, 0f)).x, 1f);
bottomWall.center = new Vector2 (0f, mainCam.ScreenToWorldPoint (new Vector3( 0f, 0f, 0f)).y - 0.5f);
leftWall.size = new Vector2(1f, mainCam.ScreenToWorldPoint(new Vector3(0f, Screen.height*2f, 0f)).y);;
leftWall.center = new Vector2(mainCam.ScreenToWorldPoint(new Vector3(0f, 0f, 0f)).x - 0.5f, 0f);
rightWall.size = new Vector2(1f, mainCam.ScreenToWorldPoint(new Vector3(0f, Screen.height*2f, 0f)).y);
rightWall.center = new Vector2(mainCam.ScreenToWorldPoint(new Vector3(Screen.width, 0f, 0f)).x + 0.5f, 0f);
}
Like the player, this walls also have RigidBody 2D and Box Collider 2D attached to it. My problem is that I'm not able to detect any kind of collision. Not even when I drag the ball up to the walls. Mine intend with this script is detect if, while dragging, you touched the walls. By doing that, I'll be able to call my game state "GameOver" and end the game.
Any idea about WHY, I'm not able to detect this collisions? I'm sure that the walls are in the right position and the ball is indeed touching the walls.
Upvotes: 0
Views: 1372
Reputation: 381
Are you just asking how to detect a collision? Make sure all of your objects (the walls and the player object) have Collider2D components. On those colliders, set IsTrigger to true. Then in your player controller:
void OnTriggerEnter2D(Collider2D other) {
// check if "other" is a wall
if (other.transform.GetComponent<Wall>())
this.gameState = GameStates.GAMEOVER;
}
Note that you need a RigidBody2D on at least one of the collidable GameObjects. Just put a RigidBody2D on everything and don't use gravity.
Upvotes: 1
Reputation: 3059
you didnt wrote how you made screenPoint so here is how just incase
public void OnMouseDown() {
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
Upvotes: 0