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 /
This question was closed May 29, 2016 at 04:30 PM by Awesome_Oposssum for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Awesome_Oposssum · May 25, 2016 at 03:10 PM · physics2deuleranglesmovetowards2d rotationangleaxis

Changing 2d Destination by angle

So I am playing with creating a 2d golf game. It's a top down and the ball only moves in the x and y directions. I have a target that shows where the ball will go.

 float x = ((target.x - transform.position.x) * (float)ratio) + transform.position.x;
 float y = ((target.y - transform.position.y) * (float)ratio) + transform.position.y;
 moveTo = new Vector2 (x, y);
 
 transform.position = Vector2.MoveTowards(transform.position, moveTo, ballspeed * Time.deltaTime);

This works just fine and the ball goes exactly where it should. The ratio is just for power so that the ball can go short of the target. So now I want to mess with accuracy. If they are off on their accuracy, I want the ball to go to the left or right (or if they are aiming straight left or right could be up and down) of the destination based on an angle and how far off they are. So this is what I came up with:

 moveTo = Quaternion.AngleAxis (accuracy, Vector3.forward) * new Vector2 (x, y);

So now the destination is changed by a set accuracy amount which goes from -30 to 30. This works perfectly most of the time. But every once and awhile the ball will shoot in the exact opposite direction or an extra 45ish degrees. I have no idea what is causing this and I feel like I am so close. Any ideas?

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

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Awesome_Oposssum · May 29, 2016 at 06:26 AM

Since I could not get what I was looking for from the built in tools (or at least could not figure it out), I used some trig with a circle as such:

 float positionX =  transform.position.x;
 float positionY = transform.position.y;

 //Find the destination based on power and the direction of the circle
 float x = ((target.x - positionX) * (float)ratio) + positionX;
 float y = ((target.y - positionY) * (float)ratio) + positionY;

 //Convert the accuracy to radians
 accuracy = offset * (Mathf.PI / 180);

 newX = positionX + ((x - positionX) * Mathf.Cos (accuracy)) + ((y - positionY) * Mathf.Sin (accuracy));
 newY = positionY + ((y - positionY) * Mathf.Cos (accuracy)) - ((x - positionX) * Mathf.Sin (accuracy));

 moveTo = new Vector2 (newX, newY);

This moves the point along a circle using the current point, the destination, and the angle difference that I want. Just for reference, for me offset is an int between -30 and 30.

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

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Find the difference between the Z rotation of two GameObjects 2 Answers

AngleAxis or Lerp: change both rotation and speed? 1 Answer

Recreating scene objects at next level 1 Answer

how to rotate to face camera point in 2D 2 Answers

Snake Movement Like in Snake vs Box 2 Answers


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