• 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
Question by DevMerlin · Feb 01, 2013 at 12:18 AM · physicsaiballpong

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.

Comment
gfvfubb

People who like this

1 Show 0
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

  • Sort: 
avatar image

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);
Comment

People who like this

0 Show 3 · 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 DevMerlin · Feb 01, 2013 at 12:39 AM 0
Share

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.

avatar image DevMerlin · Feb 01, 2013 at 12:46 AM 0
Share

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.

avatar image gfvfubb · Feb 01, 2013 at 12:55 AM 0
Share

Try this it's simpler than most of the other ones:

 var angle = Mathf.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);
 
avatar image

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);
Comment

People who like this

0 Show 2 · 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 DevMerlin · Feb 01, 2013 at 01:36 AM 0
Share

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.

alt text

avatar image robertbu · Feb 01, 2013 at 05:47 AM 0
Share

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 : MonoBehaviour {
     
     public GameObject goTrack;
     float fRadius = 4.0f;
 
     void Update () {
         
         transform.LookAt (Vector3.zero);
         float fAngle = Mathf.Atan2 (goTrack.transform.position.y, goTrack.transform.position.x);
         Vector3 v3T = new Vector3(fRadius*Mathf.Cos (fAngle), fRadius*Mathf.Sin (fAngle), 0.0f);
         transform.position = v3T;
     }

}

alt text

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.

paddleandball1.jpg (62.8 kB)

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

Vector-based Pong-ball bounce calculations 1 Answer

Simple Collision Deflection (Pong) 1 Answer

Create a "free" rolling ball controlled by player 1 Answer

Bounce ball with constant speed and height. 0 Answers

Pong game: How to make the stick affect ball's speed and direction? 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