Skip to content
Adam Graham edited this page Jul 20, 2022 · 26 revisions

Links

FAQs


The ball keeps slowing down

This usually is an indication that the ball has drag which causes it to slow down over time. If you want your ball to maintain the same velocity and not slow down, set the Linear Drag property on the Rigidbody component to zero.

If the ball slows down when hitting a wall, it is because of the friction between the two objects colliding. To prevent this friction, add a Physics Material to your ball's collider component with the Friction value set to 0 and the Bounciness set to 1. However, the physics interactions can be weird sometimes, so it doesn't always work.

If you still have problems with the ball slowing down, try adding the following function to your Ball.cs script.

private void FixedUpdate()
{
    rigidbody.velocity = rigidbody.velocity.normalized * speed;
}

This code will ensure the ball's velocity remains constant the entire time. You will probably need to decrease the value of the speed property, though. In the video, we used a value of 200, but with this new code a value of around 5 works pretty well. Feel free to adjust it to your preference.


The AI keeps missing the ball

Try changing the Mass and Linear Drag on the Rigidbody component of your AI paddle. Reducing these values allow the paddle to more easily switch directions, which will help it to be in the right place at the right time to hit the ball. Be careful though, because you can create the opposite problem where the paddle never misses. Adjust the numbers to your preference. You can also change the speed property that we added to the paddle script to make it faster or slower overall.


How do I set a maximum speed for the ball?

Before adding any additional forces to the ball, you can check what the current speed of it is and only apply the force if the speed is less than the maximum.

float speed = rigidbody.velocity.magnitude;

if (speed < maximumSpeed) {
    // apply force...
}

There are no functions available to select for the score trigger

A common mistake here is dragging the GameManager.cs file into the field rather than the "Game Manager" object in your scene. After dragging in the scene object, you should see the functions available on each component attached to that object, including the functions made available in our script.

The other common mistake is not marking your functions as public in your code. Only public functions are available in the menu. If you don't explicitly set them to be public, then they will default to private.

public void PlayerScores()
{
    //...
}

Script variables are not showing up in the editor

There are two ways for a field to show up in the editor. It can be marked public:

public float speed;

Or you can use a special attribute:

[SerializeField] private float speed;

If the variable still does not show up, this might be an indication that you have a compiler error. Your code must successfully compile before any new fields will display in the editor. Check your console for any potential errors.


Error: The namespace EventSystems does not exist

The EventSystems namespace is available in the Unity UI package, so if this error is occurring it implies that package is not installed in your project. Usually this package is installed by default, but you can manually install it from Unity's package manager, Window > Package Manager.

Alternatively, there's an even easier solution to the ScoringZone.cs script compared to the original video tutorial that does not rely on Unity's event systems. I would actually recommend using this code instead:

using UnityEngine;
using UnityEngine.Events;

public class ScoringZone : MonoBehaviour
{
    public UnityEvent scoreTrigger;

    private void OnCollisionEnter2D(Collision2D collision)
    {
        Ball ball = collision.gameObject.GetComponent<Ball>();

        if (ball != null) {
            scoreTrigger.Invoke();
        }
    }

}

Error: Can't add script component because the script class cannot be found

This is a common error that occurs if the name of the script does not match exactly the name of the class in the script. If your script is called MyClass, then the class name must be MyClass. This is also case-sensitive. Double check to make sure that this is not the issue.

If you are still running into the problem, it might help to recreate the script file again. When you create the script in the Unity editor, Unity automatically generates a class with the exact same name.

Be careful when copying other people's code into your files because it's possible the name of the class in the code might not match the name of your script. You may need to change the class name in these cases.


How do I write the "≠" symbol in the code?

In the video I had font ligatures turned on in my editor which displays certain character combinations differently. This is something I've turned off for future videos to prevent confusion for those who aren't familiar with it. The symbol is actually written as !=, which means "not equal".

Clone this wiki locally