- Home /
What is the best way to move a paddle accurately in a circle?
I'm making a sort of circular Pong-style game where the player is on the inside of a ring and the enemy paddle is on the outside. I was figuring that I could simply use
transform.RotateAround(Vector3.zero, Vector3.up, ball.transform.position.z * 2 * Time.deltaTime);
to have the paddle follow the "ball"'s motion in a two dimensional plane, but it wasn't working exactly like that. The paddles go crazy as soon as the ball is launched, spinning wildly around the axis. I have a feeling this is the right track, but considering the ball is moving on x/z, it probably needs to take into account both. There can be multiple paddles. The whole thing needs to stay steady on a two dimensional gameplay plane, ignoring Y.
Answer by gfvfubb · Feb 01, 2013 at 12:35 AM
This makes my camera rotate pretty smoothly about a pivot world point goct. Called from update function attached to my player. You should be able to achieve the same. I also recommend googling WoW Camera Controls (it has lots of rotation methods) or checking the Controller section of the Wiki Unity script catalogue.
You might want Quaternion.LerpAngle, or take a rotation * Vector(0,0,-radius) and spin the rotation. Or Lerp position, Or Slerp. Or Smoothdamp. Each of these has various benefits you can google.
ddd.x , ddd.y are from Mouse Inputs, so you can substitute for whatever you're inputting.
mct.LookAt(goct.transform);
mct.Translate(Vector3.right *ddd.x*2* -scrollSpeed * Time.deltaTime);
mct.Translate(Vector3.up *(ddd.y/1 )*-scrollSpeed * Time.deltaTime);
Thanks, I'll try that and see how this behaves. Getting this "AI" working should be interesting. I've seen it done in reverse, with the player outside but for gameplay reasons the player is inside.
Okay, no go on that. .y worked, but rotated in wrong direction while it needs to stay on a 2D plane. Substituting .z resulted in the same behaviour as RotateAround, but in more lines of code.
Try this it's simpler than most of the other ones:
var angle = $$anonymous$$athf.LerpAngle(...somesortofinputhere); // or set this to something even simpler, directly into the input for less smoothing
var radius = 10;
var qq = Quaternion.Euler(0,angle ,0);
position = qq * Vector3( 0 , 0 , -radius);
Answer by robertbu · Feb 01, 2013 at 01:15 AM
Something like this might work. "goTarget" is the game object you want to follow. This will point the forward vector towards the object but will not tilt off the xz plane.
var v3T = goTarget.transform.position - transform.position;
v3T.y = transform.position.y;
qTo = Quaternion.LookRotation(v3T, Vector3.up);
transform.rotation = gTo;
Or if you want some delay for rapid movements exchange the last line for this one:
transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, maxDegreesPerSecond * Time.deltaTime);
I figured it might be best to display a diagram of what I'm trying to do. Also, I'm working in C# - not Javascript.
Here is a bit of code I think give you what you are looking for. It places the paddle at the ball as the vector takes it from inside to outside the circle:
public class Paddle : $$anonymous$$onoBehaviour {
public GameObject goTrack;
float fRadius = 4.0f;
void Update () {
transform.LookAt (Vector3.zero);
float fAngle = $$anonymous$$athf.Atan2 (goTrack.transform.position.y, goTrack.transform.position.x);
Vector3 v3T = new Vector3(fRadius*$$anonymous$$athf.Cos (fAngle), fRadius*$$anonymous$$athf.Sin (fAngle), 0.0f);
transform.position = v3T;
}
}
It casts a ray from the center of the circle through the ball to the circle. The closer the paddle comes to the circle, the closer the paddle will be to the ball position. In a quick test, it seemed to work very well.
There are a couple of other ways you can solve this problem. You have a ray inside the circle and you need to know where on that circle the ray will intersect (so you can Slerp it into position). If you do a Google search for something like "Circle Intersecting with Ray Inside," you will find several worked out solutions including at least a couple worked out with code (not sure what language).
Another way to solve this problem is to use the Law of Cosines. If you want to go this route and need more information I can add a sketch and a bit code.
Your answer
Follow this Question
Related Questions
Vector-based Pong-ball bounce calculations 1 Answer
Simple Collision Deflection (Pong) 1 Answer
How could I make a bouncy object move mostly along the Z axis? 1 Answer
Bouncing Ball and Walls 1 Answer
Get a free moving ball. 1 Answer