Hi, I working on lectures 78 and got some problem with the code is not working on my Unity.
The box of Random Factor is not showing in my Ball Script window and I try to check everything by myself that I write the code and character correctly but I still can’t find why is it and it shows an error. Even though I copy and paste the code they come in the lectures it still not fix.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour
{
// 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 refrences
AudioSource myAudioSource;
Rigidbody2D myRigidBody2D;
// Start is called before the first frame update
void Start()
{
paddleToBallVector = transform.position - paddle1.transform.position;
myAudioSource = GetComponent<AudioSource>();
myRigidBody2D = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if (!hasStarted)
{
LockBallToPaddle();
LaunchOnMouseClick();
}
}
private void LaunchOnMouseClick()
{
if (Input.GetMouseButtonDown(0))
{
hasStarted = true;
myRigidBody2D.velocity = new Vector2(xPush, yPush);
}
}
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;
}
}
}