- Home /
unity 3d pong improve AI
I'm totally new to coding and I'm trying to create a ping pong game.
I scripted that the enemy paddle position to move with the ball position. (very simple)
public float speed = 6f;
Vector2 player2;
Vector2 player2position;
GameObject ballpos;
void Start ()
{
ballpos = GameObject.FindGameObjectWithTag ("ball");
}
void Update()
{
player2 = Vector2.Lerp (gameObject.transform.position , ballpos.transform.position , Time.deltaTime * speed );
player2position = new Vector2 ( 3.1f , Mathf.Clamp ( player2.y , -2.4f, 2.4f)) ;
gameObject.transform.position = new Vector2 ( 3.1f , player2position.y );
}
What is code do is that the ""center"" of the enemy paddle will move with respect to the ""center"" of the ball.
Now what I'm trying to do is to let the enemy hit the ball randomly to different directions, so that collision between the enemy paddle and the ball happened on the edges of the enemy paddle. (not always with the center).
Also, I have set a distance between the ball and the enemy paddle, so that when if the distance > 3 the enemy paddle will stop.
if (Mathf.Abs (ballpos.transform.position.x - paddle.transform.position.x) > 3)
{
speed = 0f;
}
if (Mathf.Abs (ballpos.transform.position.x - paddle.transform.position.x) < 3)
{
speed = 6f;
}
can you please help me with this. I have been trying to do this about two days.
Thanks :)
Your answer

Follow this Question
Related Questions
Need Help making Pong AI beatable 1 Answer
How can i avoid the enemy to sight me through walls? 1 Answer
Having AI Accelerate Up & Down 0 Answers
Coroutines randomly stop working 0 Answers
How to destory ai dead bodies in paragon shooter ai? 0 Answers