void OnCollisionEnter(Collision collission) { if (collission.gameObject.name == "Left" || collission.gameObject.name == "Right") { _started = false; _rigidbody.freezeRotation = true; _rigidbody.velocity = new Vector3(0f, 0f, 0f); gameObject.transform.position = _startPosition; } else if (collission.gameObject.name == "Top") { var left = _rigidbody.velocity.x < 0; var direction = new Vector3(left ? -1f : 1f, 0f, -1f); _rigidbody.velocity = direction * _speed; } else if (collission.gameObject.name == "Bottom") { var left = _rigidbody.velocity.x < 0; var direction = new Vector3(left ? -1f : 1f, 0f, 1f); _rigidbody.velocity = direction * _speed; } PaddleBehavior paddle = collission.gameObject.GetComponent <PaddleBehavior>(); if (paddle != null) { var bottom = _rigidbody.velocity.z < 0; var direction = new Vector3(paddle._player == Player.First ? 1f : -1f, 0f, bottom ? -1 : 1f); _rigidbody.velocity = direction * _speed; } }
// Use this for initialization void Start() { // Find the paddle object in the scene paddle = GameObject.Find("Paddle").GetComponent <PaddleBehavior>(); // Find initial distance between ball and paddle paddleToBallVector = this.transform.position - paddle.transform.position; // Get rigidbody of ball body = this.GetComponent <Rigidbody2D>(); hasStarted = false; }
// Use this for initialization void Start() { _paddleBehavior = GameObject.FindGameObjectWithTag("Player").GetComponent<PaddleBehavior>(); speed = 2f; }