Farthest player to ball in linear motion

Hi,

I want to disable the afar player control script and enable the closest player to the ball since it doesn’t work very properly as at a certain distance the player which has disabled keeps on moving in a linear motion when I take control of the player which is close to the ball. I created an empty Object and added a script to it:

public class Characters : MonoBehaviour
{
     private Transform player1;
    private Transform player2;
    private Transform Ball;

    public float pickUpDistance1;
    public float pickUpDistance2;
 
    void Start()
    {
            Ball = GameObject.Find("Ball").transform;
           player1 = GameObject.Find("Ch38_nonPBR").transform;
           player2 = GameObject.Find("Ch38_nonPBR (1)").transform;
     }

     void Update()
     {
           pickUpDistance1 = Vector3.Distance(player1.position, Ball.position);
           pickUpDistance2 = Vector3.Distance(player2.position, Ball.position);

           if (pickUpDistance1 < pickUpDistance2)
           {
                   player1.GetComponent<PlayerMovementX>().enabled = true;
                   player2.GetComponent<PlayerMovementX>().enabled = false;            
            }
            else
            {
                   player2.GetComponent<PlayerMovementX>().enabled = true;
                   player1.GetComponent<PlayerMovementX>().enabled = false;  
            }
      }
}

Thank you…

Don’t use Gameobject.Find(), just assign the scripts.

If you don’t know how then read this blurb…

Start with tutorials. LOTS of them! It’s all out there—all of it. You just need to make the effort to look for it.

Here’s a simple guide to maximize your success:

How to do tutorials properly: Two (yes, only two) simple steps.

Step 1: Follow the tutorial. Every single step. Precisely. No shortcuts! No “I think I understand this part so I’ll just skip ahead.” Even the tiniest deviation—even one character—usually leads to disaster. Welcome to the world of software engineering. Every step, every letter, punctuation, spacing (or lack thereof) must be absolutely perfect! This is how it works. It’s not hard, but it’s also not optional.

You must be perfect in everything you do here. Just be a robot. A perfect, flawless robot.

If you get any errors (and you will), don’t just post here like it’s someone else’s problem. Fix it. Learn how to read the error code. Google it, because that’s the point of Google. Your error is probably a typo, especially if you’ve already skipped over a single character (which you shouldn’t have). It’s your mistake, and only you can fix it.

Step 2: Go back and do it again. But this time, explain it! To your dog, your houseplant—whoever. If you can’t explain it, then you haven’t learned it yet. Don’t skip ahead. Stop, fix it, understand it. You’ll probably need to dive into documentation or even rewatch parts of the tutorial (again, no shortcuts). If you miss this step, you’re just mindlessly typing code with no understanding. That’s how you end up hopelessly lost as soon as you step outside the tutorial’s narrow scope.

I know it’s hard, but you’ll learn something. Don’t rush. Take the time to really understand. This step is where most people fail. Without it, you’re just a code-typing monkey, no better than the next guy.

Oh, and just to be clear, all this assumes the tutorial is free from errors. For the top-tier creators (like Unity, Brackeys, Imphenzia, Sebastian Lague), you’re likely in good hands. But for others? Yeah, errors happen. If you think something’s wrong, check the comments. You’re almost certainly not the first person to notice.

Once you’ve nailed steps 1 and 2, everything else becomes easy. Seriously, easy.

Finally, when you hit an error, don’t rush to post here. Just fix it yourself. Here’s the secret:

No one memorizes error codes. That’s not a thing. Forget the code itself. What’s useful is the complete error message. That’s your goldmine.

Pay attention to the description, the file, the line number. That’s what you need to fix it.

Start with the first error. Fix that one. The rest will likely sort themselves out. And please, check the documentation. Is what you’re trying to do even possible? Are you using it correctly?

Everything you need to solve the issue is right there in the error message. Learn to identify it fast so you can keep moving forward.

1 Like