Hi
I am working on a pong game I have make a script for moving the player palled but I have never see a script for computer moving objects (just a turrent controller script).
so I need a link to some where there I can read about it or can anny one explain how I cna make one?
//Txzzo
It is basically an AI, isn’t it? The computer player should do the same thing as a human, read the ball’s angle and speed, then calculate where it will be. The computer would have a movement speed and then would move until it was at the appropriate position.
The hard part of this AI is making it feel like a human. Making it always win would be quite easy.
okey and it shod just move vertical in the game
I could imagine making it win is very easy - probably adjusting its speed would make it more beatable, so it wouldn’t reach an area quick enough.
Though I’ve never done AI programming using Unity, you could do something like:
if object is gonna land at higher Y value (pretending Y is vertical)
then move paddle higher
if object is gonna land at lower Y value
then move paddle lower
I’m sure it’s harder than I made it sound. ![]()
okey.
well I have never make a AI script before so i dont know where to beguin.
oo by the way I can only script in Java.
//txzzo
AI is not magic, it is just a bunch of math like the rest of programming. If I made an algorithm that guesses numbers, it is a form of AI. If it reacts to “higher” and “lower” as hints, it is better. If it looks at history, that is even better.
In your case, just write a formula to find where the ball will be when it reaches the line on which your computer player can move. You have the angle, that is enough to do the calculation.
You should try to write that yourself. If you won’t, then you should just quit programming entirely. In the meantime, go read up on some basic math on how to calculate line intercepts. You’ve already been told where to begin, twice actually. So stop the excuses, plan your method and write it. If it doesn’t work, evaluate it, then fix it.
Here’s a good place to start: Unity - Scripting API:
Then take a peek at various example scripts given in the Standard Assets Package.
I would think the hardest part about this is the fact that it’s 2D - Unity3d is more of a 3D engine.
You could google “Evac City” and scroll down a bit for the tutorial, which you could maybe utilize since it uses a 2D playing field (for motion, which is different in 2D than in 3D):
That Unity is natively 3D is not relevant. Just restrict movement to a plane. For pong and the sake of practice, using physics from Unity is not the best way to learn.
what shod I read?
Common Operations
Keeping Track of Time
Accessing Other Components
Accessing Other Game Objects
Vectors
Member Variables Global Variables
Instantiate
Coroutines Yield
Writing Scripts in C#
The most important classes
Performance Optimization
Script compilation (Advanced)
//txzzo
depends on how new you are to programming for Unity.
You should read all of it, if you want to be moderately good at Unity.
well I guess I read it all.
now I have read all.
but I dident found anny about AI but I will look around some more I write later if I still need help
I don’t believe you read it all and you didn’t read what I wrote earlier about AI. AI is not magic, it is just more programming. That’s it. There is nothing in Unity that is AI specific. You have to write it using your preferred language. No matter how many ways your phrase the same question, it does not change the answer. God forbid you have to actually write some code.
I have read it all like I said
And you still missed the AI editor? The one that you just drag a component on it and basically tell it what you want the AI to do with easy buttons and stuff?
Try to think what the enemy (or whatever you want to give an AI to) needs to do, and then think how will you program it.
For example:
The enemy needs to intercept the ball… so you need to calculate the trajectory of the ball after the player hit it, to know where to move the enemy.
When you get all the information, you must will move the enemy.
I guess you have a little problem : you don’t know how to develop and pretend to find a magic script which does what you need.
Learn basics of developing.
I dont know the basic script well I know some.
thats why I make this game just to learn basic scripting.
Unity won’t provide you a good context to learn scripting because it expects you know scripting already.
Unity is like a pot: to prepare some meal, you need to mix ingredients like 3d and developing. Before to mix, you need get them first.
what is the best way to learn script than?