Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by _bearzerk · Jun 22, 2016 at 05:10 AM · c#aipong

Having AI Accelerate Up & Down

So I'm working on Pong, just something simple to get myself introduced to Unity and I'm on the last bit of the gameplay (next up menus!).

Anyway, I was just wondering, how can I tell the AI to accelerate up to top speed and down to bottom speed just to get the movement to be a heck of a lot more smooth?

Here's what I have so far:

 using UnityEngine;
 using System.Collections;
 
 public class AIScript : MonoBehaviour {
 
     //Create a magnitude for the paddles velocity later
     public float speed = 5.0f;
     //Create a ball object to follow the height value of
     public GameObject ball;
     //Create a paddleheight variable to allow for simple calculations laterS
     private float aiHeight;
     //Set up a temporary RacketDistance value to keep the FixedUpdate solution more simple.
     private float tempRacketDist;
     //Set private bool to determine previous racket direction
     private bool velup;
 
     //Called on Instantiation of the AI Racket
     void Start() {
         aiHeight = GetComponent<Renderer> ().bounds.size.y;
     }
 
     void FixedUpdate() {
         //IF the ball is travelling TOWARD the AI Racket
         if (ball.GetComponent<Rigidbody2D> ().velocity.x > 0) {
             //Find out the Racket Distance Value
             RacketDistance();
             //If it's positive
             if(tempRacketDist > (0.5f*aiHeight)) {
 
 
                 GetComponent<Rigidbody2D>().velocity = Vector2.up * speed;
                 velup = true;
 
 //Otherwise it's in the negative direction
             } else if (tempRacketDist < (-0.5f*aiHeight) ) {
 
 
                 GetComponent<Rigidbody2D>().velocity = Vector2.down * speed;
                 velup = false;
 
 //If the ball is in the centre of the racket
             } else if ( tempRacketDist == 0 || tempRacketDist < (0.5f*aiHeight) && tempRacketDist > (-0.5f*aiHeight) ) {
                 //If last direction was up
                 if(velup) {
                     //Continue up but have 1/3 speed
                     GetComponent<Rigidbody2D>().velocity = Vector2.up * (0.3f * speed);
                 //If last direction was down
                 }else{ 
                     //Continue down but at 1/3 speed
                     GetComponent<Rigidbody2D>().velocity = Vector2.down * (0.3f * speed);
                 }
                 //GetComponent<Rigidbody2D>().velocity = Vector2 * (1/5*speed);
 
             }
         } else {
             //IF the ball isn't travelling TOWARD the AI Racket, set it's velocity to zero
             this.GetComponent<Rigidbody2D>().velocity = Vector2.zero;
         }
     }
 
     void RacketDistance() {
         //Positive value means ball is above the mid-point of the racket
         //0 value means ball is at the centre of the racket, just return it with a y=0 vector
         //Negative value means ball is below the mid-point of the racket
         //
         //NOTE - This is a repurposed rip-off of the HitFactor function in the BallScript.cs file
         tempRacketDist = (ball.transform.position.y - this.transform.position.y); 
     }
 }
 
Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Need Help making Pong AI beatable 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

EnemyAI C# Error 1 Answer

AI Look at X axis more efficient code 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