ACTIVITY 2 AND 3
PASS ME THE BALL
A 2D UNITY GAME
The right player scores a point if the ball hits the left wall. The left player scores a point if the ball hits the right wall. If it hits the top or bottom wall then it bounces off. Each player will have a racket that can be moved up and down to hit back the ball.
KEYS
FIRST PLAYER: W - UP SECOND PLAYER: ↑ - UP
S - DOWN ↓ - DOWN
STEPS IN MAKING IN UNITY
Let's add four walls to our game. All we need to make a wall is a so called Sprite, or in other words: a Texture.We will use one horizontal Sprite for the top and bottom walls and one vertical Sprite for the left and right walls Let's select both wall images and then take a look at the Inspector, where we will apply the following Import Settings:
drag the walls to the project area "twice" to create a solid wall. After dragging select all 4 in the hierarchy so we can add collider.click on the Add Component button in the Inspector and then start typing Box Collider 2D
Let's add the dotted line in the middle and We will select it in the Project Area and then apply the same Import Settings.Afterwards we can drag it from the Project Area into the Scene.
using System.Collections;
using UnityEngine;
public class MoveRacket : MonoBehaviour
{
public float speed = 30;
public string axis = "Vertical";
void FixedUpdate()
{
float v = Input.GetAxisRaw(axis);
GetComponent<Rigidbody2D>().velocity = new Vector2(0, v) * speed;
}
}
And now the Pingpong Ball. Just use the import setting that we use and drag it in the Project Area, Add BoxCollider2D, and now we will create Physics 2D Material which we will name BallMaterial and put 1 in bounciness to modify Then we drag the material from the Project Area into the Material slot of the Ball's Collider,In order to make our ball move through the game world, we will add a Rigidbody2D
using UnityEngine;
using System.Collections;
public class Ball : MonoBehaviour
{
public float speed = 30;
void Start()
{
// Initial Velocity
GetComponent<Rigidbody2D>().velocity = Vector2.right * speed;
}
float hitFactor(Vector2 ballPos, Vector2 racketPos,
float racketHeight)
{
// ascii art:
// || 1 <- at the top of the racket
// ||
// || 0 <- at the middle of the racket
// ||
// || -1 <- at the bottom of the racket
return (ballPos.y - racketPos.y) / racketHeight;
}
void OnCollisionEnter2D(Collision2D col)
{
// Note: 'col' holds the collision information. If the
// Ball collided with a racket, then:
// col.gameObject is the racket
// col.transform.position is the racket's position
// col.collider is the racket's collider
// Hit the left Racket?
if (col.gameObject.name == "RacketLeft")
{
// Calculate hit Factor
float y = hitFactor(transform.position,
col.transform.position,
col.collider.bounds.size.y);
// Calculate direction, make length=1 via .normalized
Vector2 dir = new Vector2(1, y).normalized;
// Set Velocity with dir * speed
GetComponent<Rigidbody2D>().velocity = dir * speed;
}
// Hit the right Racket?
if (col.gameObject.name == "RacketRight")
{
// Calculate hit Factor
float y = hitFactor(transform.position,
col.transform.position,
col.collider.bounds.size.y);
// Calculate direction, make length=1 via .normalized
Vector2 dir = new Vector2(-1, y).normalized;
// Set Velocity with dir * speed
GetComponent<Rigidbody2D>().velocity = dir * speed;
}
}
}
If we press play, we can now influence the ball's bouncing direction depending on where we hit it with the Racket.
Comments
Post a Comment