Pong
Controls
ws
Github
https://github.com/katpavan/pong
Ideas
- after you hit ball, hit spacebar to change it's direction once to fool the player. it takes x amount of time to change directions and changes colors before it does
- make it a betting game. computer vs computer, characters chosen at random
- you get some fake currency make bets and first to 3 wins. it turns into an idle game lol.
Learned/Notes
- made GameManager a singleton and called the GameManager instance ResetRound function in the Ball Start function
- inheritance
- I made a Paddle class and extended it with a ComputerPaddle and PlayerPaddle class
- only apply force if we're moving in a direction
- if (direction.sqrMagnitude != 0)
- Create -> 2D -> Physics Material 2D
- FixedUpdate is where you almost always do physics related logic
- wrote some AI logic for the computer paddle
- used the ball's position to determine whether the paddle should go up or down
- if ball is moving away from computer paddle, then computer paddle goes idle in the center
- made a 2d physics material and put it on the 2d rigid body component of the ball
- I set friction to 0 and bounciness to 1
- get the contact point of when the ball collides with something
- Vector2 normal = collision.GetContact(0).normal;
- there could be multiple contact points, so we just want the first one
- Vector2 normal = collision.GetContact(0).normal;
- add a force to the ball based on the contact point of collision above
- ball.AddForce(-normal * this.bounceStrength);
- we do negative here because we need to add the force in the opposite direction
- ball.AddForce(-normal * this.bounceStrength);
- Reset ball position and vector after a score
- _rigidbody.position = Vector3.zero;
_rigidbody.velocity = Vector3.zero;
- _rigidbody.position = Vector3.zero;
to detect if the ball went into a scoring zone, I used EventTrigger and put the script as a component onto the left and right walls of the game.
- using UnityEngine.EventSystems;
- public EventTrigger.TriggerEvent scoreTrigger;
- use the following inside OnCollisionEnter2D
- BaseEventData eventData = new BaseEventData(EventSystem.current);
- this.scoreTrigger.Invoke(eventData);
- when the ball collides with our scoring zone, we trigger an event
- used TextMeshPro for the text
- using TMPro;
- [SerializeField] TextMeshProUGUI computerScore;
- computerScore.text = _computerScore.ToString();
Leave a comment
Log in with itch.io to leave a comment.