Ball Not Launching

If someone could please advise. Seems to be right, I tried going in adjusting, looking over unity, and googling, but still does not seem to work. Thank you

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Ball : MonoBehaviour
{

//config parameters
[SerializeField] Paddle paddle1;

//state
Vector2 paddleToBallVector;

// Start is called before the first frame update
void Start()
{
    paddleToBallVector = transform.position - paddle1.transform.position;
}

// Update is called once per frame
void Update()
{
    LockBallToPaddle();
    LaunchOnMouseClick();
}

private void LaunchOnMouseClick()
{
    if (Input.GetMouseButtonDown(0))
    {
        GetComponent<Rigidbody2D>().velocity = new Vector2 (2f, 15f);
    }
}

private void LockBallToPaddle()
{
    Vector2 paddlePost = new Vector2(paddle1.transform.position.x, paddle1.transform.position.y);
    transform.position = paddlePost + paddleToBallVector;
}

}

Hi JoshHere,

Have you already tried to add Debug.Logs to your code to see what is going on during runtime? For instance, does the LaunchOnMouseClick ever get called?

It seems fine, though you’re missing the bool to disable LockBallToPaddle, which is shown at the end of the video.

it does take the command when i do Debug.Log(“Click”) the message is showing up in the console

but it is not taking GetComponent().velocity = new Vector2 (2f, 15f);

I could be wrong, it looks like you’re locking the ball to the paddle every single frame and never unlocking it. You should be using a bool to determine whether to lock the ball or not so that you’re not trying to launch the ball but keeping it locked to the paddle on the same frame. Remember, Update() is called each and every single frame your game is running, and all code being executed by it will be ran every single frame as well. Basically, what LanceIV said.

got it to work, thanks for all the help

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms