Problem with autoplay

With autoplay turned on, my paddle will not move or if it does, it just barely moves and then the ball just very quickly falls into the lose collider.

Here is my code for my paddle.cs:


    //configuration parameters
    [SerializeField] float minX = 1f;
    [SerializeField] float maxX = 15f;
    [SerializeField] float screenWidthInUnits = 16f;

    //cached references
    GameSession theGameSession;
    Ball theBall;
    
	// Use this for initialization
	void Start () {
        theGameSession = FindObjectOfType<GameSession>();
        theBall = FindObjectOfType<Ball>();
	}
	
	// Update is called once per frame
	void Update () {
        Vector2 paddlePos = new Vector2(transform.position.x, transform.position.y);
        paddlePos.x = Mathf.Clamp(GetXPos(), minX, maxX);
        transform.position = paddlePos;
	}
    private float GetXPos()
    {
        if(theGameSession.IsAutoPlayEnabled())
        {
            return theBall.transform.position.x;
        }
        else
        {
            return Input.mousePosition.x / Screen.width * screenWidthInUnits;
        }
    }
}

Here is my code for my ball.cs


    // config params
    [SerializeField] Paddle paddle1;
    [SerializeField] float xPush = 2f;
    [SerializeField] float yPush = 15f;
    [SerializeField] AudioClip[] ballSounds;
    [SerializeField] float randomFactor = 0.2f;

    // state
    Vector2 paddleToBallVector;
    bool hasStarted = false;

    // Cached component references
    AudioSource myAudioSource;
    Rigidbody2D myRigidBody2D;

	// Use this for initialization
	void Start ()
    {
        paddleToBallVector = transform.position - paddle1.transform.position;
        myAudioSource = GetComponent<AudioSource>();
        myRigidBody2D = GetComponent<Rigidbody2D>();
        myRigidBody2D.simulated = false;
	}
	
	// Update is called once per frame
	void Update ()
    {
        if (!hasStarted)
        {
            LockBallToPaddle();
            LaunchOnMouseClick();
        }
	}

    private void LaunchOnMouseClick()
    {
        if(Input.GetMouseButtonDown(0))
        {
            Debug.Log("Mouse button was pressed down");
            hasStarted = true;
            myRigidBody2D.velocity = new Vector2(xPush, yPush);
            myRigidBody2D.simulated = true;
         }
    }

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

    private void OnCollisionEnter2D(Collision2D collision)
    {
          Vector2 velocityTweak = new Vector2
            (Random.Range(0f, randomFactor), 
            Random.Range(0f, randomFactor)); 

        if (hasStarted)
        {
            AudioClip clip = ballSounds[UnityEngine.Random.Range(0, ballSounds.Length)];
            myAudioSource.PlayOneShot(clip);
            myRigidBody2D.velocity += velocityTweak;
        }
    } 
 }

And here is my code for my GameSession.cs:


    // config params
    [Range(0.1f, 10)]  [SerializeField] float gameSpeed = 1f;
    [SerializeField] int pointsPerBlockDestroyed = 83;
    [SerializeField] TextMeshProUGUI scoreText;
    [SerializeField] bool isAutoPlayEnabled;
    
    // state variables
    [SerializeField] int currentScore = 0;

    private void Awake()
    {
        int gameStatusCount = FindObjectsOfType<GameSession>().Length;
        if (gameStatusCount > 1)
        {
            Destroy(gameObject);
        }
        else
        {
            DontDestroyOnLoad(gameObject);
        }
    }
    private void Start()
    {
        scoreText.text = currentScore.ToString();
    }
    // Update is called once per frame
	void Update () {
        Time.timeScale = gameSpeed;
	}

    public void AddToScore()
    {
        currentScore += pointsPerBlockDestroyed;
        scoreText.text = currentScore.ToString();
    }

    public void ResetGame()
    {
        Destroy(gameObject);
    }
    public bool IsAutoPlayEnabled()
    {
        return isAutoPlayEnabled;
    }
}

I’m not seeing anything wrong in the code. Try checking the inspecter for the paddle and making sure the minX and maxX values haven’t been changed.

1 Like

Thanks for checking my code. The minX and maxX values are the same whether autoplay is on or off. With autoplay off, the paddle works just fine but with it on, it doesn’t move.

Hi Penny,

To see if there is a problem with the values being used for the clamping in your Update method within Paddle.cs, why not add a Debug.Log statement to output paddlePos.x, minX and maxX, for example;

void Update () 
{
    Vector2 paddlePos = new 
    Vector2(transform.position.x, transform.position.y);
    paddlePos.x = Mathf.Clamp(GetXPos(), minX, maxX);

    Debug.Log("paddlePos.x : " + paddlePos.x + " | minX : " + minX + " | maxX : " + maxX);

    transform.position = paddlePos;
}

The above is going to be called every frame so you will get a lot of output but as soon as you see the problem occurring you could pause the game to look at the values. You don’t want Collapse enabled in the Console for this.

The above would help you identify if the paddle is being constrained by that calculation, or not.

Hope this helps :slight_smile:

I tried what you said but it’s just the same output over and over again. The paddle won’t even budge. It’s like the movement of the mouse is frozen or something. I can click with the mouse and it start playing but I can move the paddle back and forth with the mouse.

Well I just tried again and it did start to move some. It’s weird – like there is a long hesitation before it will move and most of the time by the time it decides to move, I lose.

It is working some better now but maybe there are bugs that is keeping it from playing through the level now.

How can I set the game so that it doesn’t go all the back to the first level when you lose? I would like it to be where it would just go back to the level you were on instead of going all the back to the first level.
That might be a little frustrating for some players. I know it frustrates me.

Hi Penny,

I tried what you said but it’s just the same output over and over again.

What were the values? What was output to the console?

The paddle won’t even budge. It’s like the movement of the mouse is frozen or something. I can click with the mouse and it start playing but I can move the paddle back and forth with the mouse.

That is the point of autoplay though? e.g. the player doesn’t control the movement, your game controls the movement.

How can I set the game so that it doesn’t go all the back to the first level when you lose?

I would probably iron out the problems you have above before adding another feature/more complexity, happy to help with the above if needed.

Regarding your question, you would need to keep track of the level the player is currently playing, you can access that using something like this;

using UnitEngine.SceneManagement;
int lastLevelPlayed;   // class level variable in a persistant object - GameSession perhaps
lastLevelPlayed = SceneManager.GetActiveScene().buildIndex;  // populate the variable when the level loads

The, when the player runs out of lives, consider offering a “Continue” option, as well as a “Restart” option. “Restart” would load the game from the beginning, “Continue” would load the lastLevelPlayed level but obviously give the player their 3 lives etc.

You could potentially make a more generic “start game” method which takes in the scene index to load, or, if you had separate methods, which may help with readability;

public void NewGame()
{
    LoadScene(1);    // "1" is just arbitary, which ever you first playable scene index number is would go here
}

public void ContinueGame(int lastLevelPlayed)
{
   LoadLevel(lastLevelPlayed);
}

private void LoadScene(int sceneIndex)
{
    // ...
}

That kind of thing.

Hope this helps and if you want a hand resolving the initial problem you reported let me know.

Privacy & Terms