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;
}
}