I have a code that makes a paddle follow a ball along the X-axis simply by knowing the ball’s position. I planned to use this as a sort of “practice mode” for a breakout-style game but all it does is act like a wall, naturally. By that I mean it, no matter what, will be in the exact position of the ball, thus doing absolutely nothing and making it impossible to win.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AI_Paddle : MonoBehaviour {
public bool autoPlay = true;
private Ball ball;
void Start()
{
ball = GameObject.FindObjectOfType<Ball>();
}
// Update is called once per frame
void Update ()
{
AutoPlay();
}
void AutoPlay()
{
Vector3 paddlePos = new Vector3(0.5f, this.transform.position.y, -1f);
Vector3 ballPos = ball.transform.position;
paddlePos.x = Mathf.Clamp(ballPos.x, 0.5f, 15.5f);
this.transform.position = paddlePos;
}
}
I would like to make it possible to beat the practice mode using something along the lines of a speed cap so the “practice paddle” can’t be on the ball’s exact position at higher speeds (I am open to suggestions of other ways) but, being a complete beginner, I have no clue how. I’m assuming this would require the entire AutoPlay() function?