Paddle going to minX position of paddle

In AUTOPLAY mode,
when I click GamePlay button my paddle moving to minX pos of the paddle before initiating mouse click. But Rick it’s remained the same until he gave a mouse click.

Hi,

Is the z-position of the camera set to z = -10, the background to z = 10, and the rest to z = 0?

But I think it’s not the issue.
All z positions are set as you said but again behaving same…

You are right. I misunderstood your problem. :confused:

Is the camera position set to (8, 6, -10)? The correct position of the camera is crucial. The left edge must be at x = 0.

Is the camera mode set to “orthographic”?

Yeah… and camera positions are set to (8,6,-10)

I changed minX position at Start method to paddle position…
And then in Update method I changed minX to 1f.
But its stupid thing before after starting update method again taking paddle to 1f

Eurekaa !!

I just add hasStarted as like in Ball.cs now it’s working cool…

Paddle.cs updated

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

public class Paddle : MonoBehaviour
{
    bool hasStarted = false;
    // Configuration Parameters
    [SerializeField] float minX = 1f;
    [SerializeField] float maxX = 15f;
    [SerializeField] float screenWidthInUnits = 16f;

    //cached references
    GameSession theGameSession;
    ChromeBall theChromeBall;

    // Start is called before the first frame update
    void Start()
    {
        theGameSession = FindObjectOfType<GameSession>();
        theChromeBall = FindObjectOfType<ChromeBall>();
    }

    // Update is called once per frame
    void Update()
    {
        if (hasStarted)
        {
            Vector2 paddlePos = new Vector2(transform.position.x, transform.position.y);
            paddlePos.x = Mathf.Clamp(GetXpos(), minX, maxX);
            transform.position = paddlePos;
        }
        else
        {
            LaunchPaddle();
        }
    }

    private float GetXpos()
    {
        if (theGameSession.IsAutoPlayEnabled())
        {
            return theChromeBall.transform.position.x;
        }
        else
        {
            float mousePosInUnits = Input.mousePosition.x / Screen.width * screenWidthInUnits;
            return mousePosInUnits;
        }
    }

    private void LaunchPaddle()
    {
        if (Input.GetMouseButtonDown(0))
        {
            hasStarted = true;
        }
    }
}

But I don’t know why paddle moving to minX like scate board before giving a mouse click as Rick said.
Please let me know the reason.

What mouse click to you mean? To release the ball? Or just a mouse click into the game window to set the focus on the game window?

If the issue persists, I would suggest to rewatch the video and/or to log the mouse coordinates as well as the paddlePos variable into your console to see if they make sense. Also check the values in the Inspector of the Paddle component because the Inspector overrides the values at the top of your code.


See also:

I found it…

I have slight difference in X-position of Paddle: 8.04 & Ball: 8.01 this made my paddle to go to for ball position. So it’s going to min edge. That’s it…

2 Likes

To release the ball

Good job! :slight_smile:

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

Privacy & Terms