Mahi
Mahi

Reputation: 15

c# - Spacebar in Pong

With my pong game I want it so that once it hits the colliders, it will reset to its default position. It does detect the colliders and triggers. However it doesn't respond to the space button

Here is my code:

using UnityEngine;
using System.Collections;


public class BallControl : MonoBehaviour
{
    public float speed = 10.0f;
    private Rigidbody2D rbBall;
    private Vector2 defaultPos;

    void Start()
    {
        // Initial Velocity
        GetComponent<Rigidbody2D>().velocity = Vector2.left * speed;
    }

    float hitFactor(Vector2 ballPos, Vector2 racketPos,
                    float racketHeight)
    {
        return (ballPos.y - racketPos.y) / racketHeight;
    }

    void OnCollisionEnter2D(Collision2D col)
    {
        if (col.gameObject.name == "Player1")
        {
            Debug.Log("P1 HIT!!!");
            // 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 == "Player2")
        {
            Debug.Log("P2 HIT!!!");
            // 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;
        }
    }

    public void reset (string startPosition)
    {
        transform.position = defaultPos;

        rbBall = GetComponent<Rigidbody2D>();

        if (startPosition == "Right")
        {
            rbBall.velocity = Vector2.right * 0;

            if (Input.GetKeyDown(KeyCode.Space))
            {
                rbBall.velocity = Vector2.right * speed;
            }
        }

        if (startPosition == "Left")
        {
            rbBall.velocity = new Vector2(-1,0) * 0;
            if (Input.GetKeyDown(KeyCode.Space))
            {
                rbBall.velocity = new Vector2(-1,0) * speed;
            }
        }
    }
}

EDIT; So here it shows my Ball Control script. This is what I have set for the parameters and this is my code altogether. I want it to be able to reset the ball position, press the spacebar to restore the velocity and carry on the game

Upvotes: 0

Views: 182

Answers (1)

TheLogan
TheLogan

Reputation: 33

The reason it isn't working is simply because you're using the Input.GetKeyDown in the wrong place. From what I can see your Reset method only runs once, which means that the player would have to click spacebar in exactly the same millisecond that reset method is running.

What you want is to extract the "boost" the ball part out of the Reset method and add it to the Update method, something like this:

bool isReset = true;
public void Update(){
    if(isReset && Input.GetKeyDown(KeyCode.Space){
        if (startPosition == "Right"){
            rbBall.velocity = Vector2.right * speed;
        }else{
            rbBall.velocity = -Vector2.right * speed;
        }
        isReset = false;
    }
}

public void Reset (string startPosition)
{
    transform.position = defaultPos;
    isReset = true;
}

void Start(){
    rbBall = GetComponent<Rigidbody2D>();
}

From what I can see, this should do what you want.

The reason your code doesn't work is that the thread doesn't pause while waiting for the spacebar to be clicked, it checks once and then continues doing what ever else you're asking it to do.

Upvotes: 1

Related Questions