- Home /
Question by
HuskyPanda213 · Feb 25, 2014 at 08:23 PM ·
aisimplepong
Pong game AI problem.
Hello, I need some help. When I try and make a pong AI system, the opponent flies off of the screen, and in general it does not work, do you know why?
Code:
using UnityEngine;
using System.Collections;
public class AIPaddle : MonoBehaviour {
public PaddleBehave Movement;
void Start(){
if(!Movement){
Movement = GetComponent<PaddleBehave>();
}
}
void Update(){
GameObject go = GameObject.FindGameObjectWithTag("GameController");
if(go.GetComponent<GameManager>().GameStarted){
Predict_AIMovement();
}
}
private void Predict_AIMovement(){
Vector3 ballPosition = GameObject.FindGameObjectWithTag("Ball").transform.position;
float yBallPos = ballPosition.y;
float predictedPosition = yBallPos;
Movement.Move(predictedPosition,Space.World);
}
}
Code(Movement function):
public void Move(float yMovement, Space worldSpace){
transform.Translate(new Vector3(0,yMovement,0) * MoveSpeed * Time.deltaTime,worldSpace);
}
Comment
Could you please translate into a js script. i can't know c# properly. Thanks in advance..
Answer by robertbu · Feb 25, 2014 at 08:34 PM
You are mixing positions and translations. That is, you are using the balls 'y' position as a delta amount to move the paddle. Here is a bit of alternate code for you to try:
public void Move(float yMovement){
Vector3 v = transform.position;
v.y = yMovement;
transform.position = Vector3.MoveTowards(transform.position, v, MoveSpeed * Time.deltaTime);
}
Your answer
Follow this Question
Related Questions
Really simple AI that works with physics? 1 Answer
Simple AI Script help 2 Answers
What is the best way to move a paddle accurately in a circle? 2 Answers
Basic AI script. need help. 1 Answer
Pong AI/Paddle controller help 1 Answer