• 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 Plapatine · Jun 15, 2018 at 04:03 AM · aipaddle

How to make a Pong AI

I am making a simple Pong game. I have been trying to make the other paddle controlled by AI and I am running to some problems. My current solution just makes the paddle flicker and not move at all. T$$anonymous$$s is what my code looks like:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class RedPaddle : MonoBehaviour {
 
     private Ball ball;
 
     [SerializeField] float movementSpeed = 0.1f;
 
     void Start ()
     {
         ball = FindObjectOfType<Ball>();
     }
     
     void Update ()
     {
         Vector3 ballPosition = ball.transform.position;
         Vector3 paddlePosition = t$$anonymous$$s.transform.position;
         if(paddlePosition.y > ballPosition.y)
         {
             paddlePosition.y -= movementSpeed;
         }
         else if(paddlePosition.y < ballPosition.y)
         {
             paddlePosition.y += movementSpeed;
         }
 
         transform.position = paddlePosition;
     }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by SlowCircuit · Jun 15, 2018 at 05:16 AM

  1. Make sure you only have one object with the Ball component on it. Otherwise it might be getting the wrong one.

  2. Maybe try increasing movement speed and see if that changes anyt$$anonymous$$ng?

  3. Make sure not$$anonymous$$ng else is changing the position of the AI paddle. If it's flickering then it's possibly it's being moved and moved back really fast.

If it's none of those t$$anonymous$$ngs then post a screenshot of both the ball and AI paddle inspector. Also post a GIF/video of what the AI paddle does.

Comment
Add comment · Share
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
0

Answer by Plapatine · Jun 16, 2018 at 03:43 AM

@thetomfinch

  1. I t$$anonymous$$nk I am getting the right one because when I try checking whether not the if/else if statements can trigger or not by printing the console they can.

  2. I have tried a lot of different values but none of them seem have to any different effect

  3. I feel like t$$anonymous$$s might be the problem but unfortunately I have no idea how t$$anonymous$$s could be solved

I couldn't figure out a way to get the video small enough to attach it here but here are the pictures from the inspector anyway (The first one is the red paddle on the right and the other is the ball)

alt text


nayttokuva-70.png (217.6 kB)
nayttokuva-71.png (216.1 kB)
Comment
Add comment · Share
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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

146 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Pong AI/Paddle controller help 1 Answer

I need some help with AI 0 Answers

trouble with my lookAt script 0 Answers

i can't set destination, using AI Navmesh via raycast 1 Answer

ZOMBIE AI SCRIPT 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