Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Oct 23, 2013 at 02:59 AM by meat5000 for the following reason:

Question too broad for UA. Feel free to post new questions - keep it specific with relevant snippets of code; Concise questions, Concise Answers

avatar image
0
Question by ConfirmCrit · Oct 22, 2013 at 09:13 PM · c#beginnerpong

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;
         
     }
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image meat5000 ♦ · Oct 22, 2013 at 09:14 PM 0
Share

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 :)

avatar image robertbu · Oct 22, 2013 at 09:40 PM 1
Share

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.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

What is wrong with this code for adjusting the walls and players to match the screen size? 1 Answer

How would I make a ListChangedEvent? 1 Answer

Problem with rotation Please Help Me 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges