Hai Guys,
I am a newbie, i want to make pong simple game for ai vs ai but i dont know how to make it. Im just know how to make player vs player. Can anyone tell me how to make it ? i have allready upload my paddle code below
Can you paste the code here with code tags, it is really hard to read it from those images.
[code=CSharp]using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Paddle : MonoBehaviour
{
// Start is called before the first frame update
float speed = 20;
float height;
string input;
public bool isRight;
Random rand = new Random();
void Start()
{
height = transform.localScale.y;
}
public void Init(bool isRightPaddle){
isRight = isRightPaddle;
Vector2 pos = Vector2.zero;
if(isRightPaddle){
pos = new Vector2(GameManager.topright.x, 0);
pos = pos - Vector2.right * 3 * transform.localScale.x;
input = "PaddleRight";
}
else
{
pos = new Vector2(GameManager.bottomleft.x, 0);
pos = pos + Vector2.right * 3 * transform.localScale.x;
input = "PaddleLeft";
}
transform.position = pos;
transform.name = input;
}
// Update is called once per frame
void Update()
{
float move = Input.GetAxis(input) * Time.deltaTime * speed;
if(transform.position.y < GameManager.bottomleft.y + height / 2 && move < 0){
move = 0;
}
if(transform.position.y > GameManager.topright.y - height / 2 && move > 0){
move = 0;
}
transform.Translate(move * Vector2.up);
}
}
[/code]
done man @eses
There doesn’t seem to be any code for AI yet?
If you don’t know how to make it, try to break it down to smallest elements -
I’m not going to write it for you, and I haven’t ever done such logic, but I guess it could be as simple as this (a naive solution):
- Get reference to active/current ball.
- When ball moves vertically, computer paddle follows it vertically.
- If ball moves up, paddle moves up and only moves as far as the ball does in Y-axis. Same logic for down move.
Of course this does look pretty predictable, but it will get you started.
You could also start predicting the location of ball on line that is defined by computer paddle x position and its vertical movement line. Then you can use ball velocity (= used as direction) to detect where it will end up on this line.
I bet there are several existing code samples available for major programming languages that you can also read.