private void OnCollisionEnter(Collision other) { if (other.gameObject.tag == "Player") { if (other.gameObject.layer == LayerMask.NameToLayer("LocalPlayer")) { ballManager.SetStatus(BallManager.BallStatus.me); } if (other.gameObject.layer == LayerMask.NameToLayer("ConnectedPlayer")) { ballManager.SetStatus(BallManager.BallStatus.other); } float y = hitFactor(transform.position, other.transform.position, other.collider.bounds.size.y); if (other.gameObject.transform.position.x < 0) // left { Vector3 newDirection = new Vector3(1, y).normalized; ballMovement.MoveBall(newDirection); return; } if (other.gameObject.transform.position.x > 0) // right { Vector3 newDirection = new Vector3(-1, y).normalized; ballMovement.MoveBall(newDirection); return; } } }
private void BounceFromRacket(Collision2D collision) { var ballPosition = this.transform.position; var racketPosition = collision.collider.transform.position; var racketHeight = collision.collider.bounds.size.y; float x; if (collision.collider.name == "RacketPlayer1") { x = 1; } else { x = -1; } float y = (ballPosition.y - racketPosition.y) / racketHeight; ballMovement.IncreaseHitCount(); ballMovement.MoveBall(new Vector2(x, y)); }
public ScoreController scoreController; //We got an instance of the score controller because this is where we increment our points void BounceFromRacket(Collision2D c) { Vector3 ballPosition = transform.position; Vector3 racketPosition = c.gameObject.transform.position; float racketHeight = c.collider.bounds.size.y; //Returns the height of the bounds of our racket. float x; if (c.gameObject.name == "Player 1") { x = 1; //Towards the right } else { x = -1; //Torwards the left } float y = (ballPosition.y - racketPosition.y) / racketHeight; ballMovement.IncreaseHitCounter(); //We increase our hit counter so the speed of our ball is increased ballMovement.MoveBall(new Vector2(x, y)); //We are adding in the new direction the ball should be going towards. Plus we increase the counter before calling this }
void BounceFromRecket(Collision2D collision) { Vector3 ballPosition = transform.position; Vector3 recketPosition = collision.gameObject.transform.position; float recketHeight = collision.collider.bounds.size.y; float x; if (collision.gameObject.name == "RecketPlayer1") { x = 1; } else { x = -1; } float y = (ballPosition.y - recketPosition.y) / recketHeight; ballMovement.IncreaseHitCounter(); ballMovement.MoveBall(new Vector2(x, y)); }