- Home /
Question too broad for UA. Feel free to post new questions - keep it specific with relevant snippets of code; Concise questions, Concise Answers
Questions on improving my pong code
I'm currently doing a game jam on GameDev.net, where I have to modify pong. I currently have a rough version up, but there's some strong issues with it. A) I can't find a way for my current set up of the AIPaddle to be easier against the player. I tried using translate instead of position, but I couldn't get it to properly read the ball, but I can with position.
B)My ball spawning system might need work, it's causing issues with everything that depends on the ball.
C) I spent 8-10 hours trying to use GetComponent to make ONE function to reset the game, instead of three, one for the ball, two for each paddle. No matter how I tried, it would either not properly compile, or just not work when testing. Intellisense was picking up everything fine, so I don't know what I was doing wrong.
Thanks for any aid. :D AIPaddle.CS
using UnityEngine;
using System.Collections;
public class aiPlayer: MonoBehaviour {
public KeyCode key_up;
public KeyCode key_down;
public Vector3 pad2DefPos;
public Transform tBall;
GameObject Ball;
GameObject paddle2;
float nextMove;
float moveDelay = .5f;
float delayDelay = 5f;
// Use this for initialization
void Start () {
pad2DefPos = this.transform.position;
paddle2 = this.gameObject;
nextMove = Time.time + moveDelay;
}
// Update is called once per frame
void Update () {
Ball = GameObject.FindGameObjectWithTag("ball");
tBall = Ball.transform;
//if(Input.GetKeyDown(KeyCode.Space))
//{
// pad2Reset();
//}
if (Time.time > nextMove)
{
nextMove = Time.time + moveDelay;
transform.position = new Vector3(pad2DefPos.x,
tBall.transform.position.y,
pad2DefPos.z);
}
//if (tBall.position.y >= transform.position.z + 3){
//transform.Translate(o, .5, 0);
}
//rigidbody.velocity = new Vector3(0, 0.2f,0);
}
void FixedUpdate() {
// get the current position
Vector3 pos = transform.position;
if (Input.GetKeyDown(KeyCode.Space))
{
pad2Reset();
}
if (Input.GetKey(key_up))
{
// player wants to move the racket upwards
transform.position = new Vector3(pos.x,
pos.y + 0.5f,
pos.z);
}
else if (Input.GetKey(key_down))
{
// player wants to move the racket downwards
transform.position = new Vector3(pos.x,
pos.y - 0.5f,
pos.z);
}
}
public void pad2Reset()
{
paddle2.transform.position = pad2DefPos;
}
}
Ball.cs using UnityEngine; using System.Collections;
public class Ball : MonoBehaviour
{
private Vector3 direction;
private Vector3 defPos;
public Rigidbody ball;
public GameObject paddle1;
private Paddle paddle1Script;
private float speed;
//public Paddle paddle1;
public bool go;
// Use this for initialization
void Start ()
{
paddle1Script = paddle1.GetComponent<Paddle>();
this.direction = new Vector3(1.0f, 1.0f, 0).normalized;
this.speed = .5f;
go = false;
defPos = this.transform.position;
// defRot = this.transform.rotation;
}
// Update is called once per frame
void Update ()
{
Vector3 pos = transform.position;
pos.z = 0;
transform.position = pos;
if (Input.GetKeyDown(KeyCode.Space))
{
go = true;
}
if (go == true)
{
this.transform.position += direction * speed;
}
if (this.transform.position.x > 30)
{
ballReset();
// player1Paddle.pad1Reset();
}
if (this.transform.position.x < -30)
{
ballReset();
//player1Paddle.pad1Reset();
}
}
void OnCollisionEnter(Collision collision)
{
Vector3 normal = collision.contacts[0].normal;
direction = Vector3.Reflect(direction, normal);
}
public void ballReset()
{
go = false;
//speed = speed * -1;
//ball.transform.position = defPos;
Instantiate(ball, new Vector3(0,0,0), Quaternion.identity);
DestroyObject(gameObject);
//DestroyObject(paddle1);
}
}
Paddle.cs using UnityEngine; using System.Collections;
public class Paddle: MonoBehaviour {
public KeyCode key_up;
public KeyCode key_down;
public Vector3 pad1DefPos;
public GameObject ball;
//GameObject paddle1;
// Use this for initialization
void Start ()
{
pad1DefPos = this.transform.position;
// paddle1 = this.gameObject;
}
// Update is called once per frame
void Update ()
{
//
if(Input.GetKeyDown(KeyCode.Space))
{
pad1Reset();
}
}
void FixedUpdate()
{
// get the current position
Vector3 pos = transform.position;
if (Input.GetKey(key_up))
{
// player wants to move the racket upwards
transform.position = new Vector3(pos.x,
pos.y+ 0.5f,
pos.z );
}
else if (Input.GetKey(key_down))
{
// player wants to move the racket downwards
transform.position = new Vector3(pos.x,
pos.y- 0.5f,
pos.z );
}
}
public void pad1Reset()
{
this.transform.position = pad1DefPos;
}
}
Good job formatting the code. If you take out a lot of the spaces it'd look a lot smaller though. CodeStorms can put people off :)
UA works best when there is a single issue per question. I suggest you break your question into three questions with the appropriate source for each question.