Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Astelot · Jun 25, 2015 at 01:34 AM · mouserotatearoundcircle

Rotating around a point in a circular motion depending on mouse position

Hello everyone,

Im trying to create sort of aiming system for my top down shooter but can't get it to work. I've done tons of research but I didn't find anything usefull.

What I'm trying to do, is to create a circle around a character with a dot on it, and then make that dot rotate around the player to "face" the mouse, "get as close as possible to it".

I got the behavior I like, but it feels unrealistic, and I get wierd teleportations etc etc. I would like to set up a fixed speed of the dots rotation, lets say 2pi in a second, but I have no idea how to achive this.

I dont know, it feels just to wierd and unnatural, and I want to make it feel smooth and kind of "real", like its an actual physic thing.

Here is the code:

 public class Test1 : MonoBehaviour {
     
     public Transform target; 
 
     public float speed;
     public float fRadius = 3.0f;
     public float targetAngle;
     public float currentAngle;
     
     float x;
     float y;
 
     void Start () {
 
     }
 
     void Update () {
 
         Vector3 mousePosition2D = Camera.main.WorldToScreenPoint (target.position);
         mousePosition2D = Input.mousePosition - mousePosition2D;
 
         currentAngle = Mathf.Atan2 (transform.position.y, transform.position.x) * Mathf.Rad2Deg;
 
         targetAngle = Mathf.Atan2 (mousePosition2D.y, mousePosition2D.x) * Mathf.Rad2Deg;
 
         mousePosition2D = Quaternion.AngleAxis (targetAngle, Vector3.forward) * (Vector3.right * fRadius);
         //Debug.Log (mousePosition2D);
         transform.position = target.position + mousePosition2D;
     }


And what I'm trying to achive:

alt text

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

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by zach-r-d · Jun 25, 2015 at 02:15 AM

Working manually with inverse trig functions is really tricky and takes forever to get right; I strongly recommend using vectors instead. In this case, the desired behavior can be accomplished using Vector3.RotateTowards:

 Vector3 targetScreenPos = Camera.main.WorldToScreenPoint (target.position);
 targetScreenPos.z = 0;
 Vector3 targetToMouseDir = Input.mousePosition - targetScreenPos;
 
 Vector3 targetToMe = transform.position - target.position;
 targetToMe.z = 0;
 
 Vector3 newTargetToMe = Vector3.RotateTowards(targetToMe, targetToMouseDir, /* max radians to turn this frame */, 0f);
 
 transform.position = target.position + /*distance from target center to stay at*/ * newTargetToMe.normalized;
Comment
Add comment · Show 4 · 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 Astelot · Jun 25, 2015 at 02:39 AM 1
Share

Works like a charm! Thank you. Never heard of that function before. Have to write it down :D

avatar image zach-r-d · Jun 25, 2015 at 03:16 AM 0
Share

I'm glad it works! If the answer is satisfactory, remember to accept it as well. :)

How to accept an answer

avatar image Astelot · Jun 25, 2015 at 06:03 AM 0
Share

If I will have more comments about this issue, am I free to P$$anonymous$$ you? Thanks for help once again.

avatar image zach-r-d · Jun 25, 2015 at 06:58 AM 0
Share

If a new problem regarding this comes up post a new question here on UA, but feel free to mention me by writing "@zach.r.d" in the question so that I am personally notified.

That way, if someone else can answer it before I do, you will get a faster response, otherwise, I will answer it when I see it and it would be as if you sent me a P$$anonymous$$!

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.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Drawing Sector around GameObject in GameView: GUITexture or 3D Object 1 Answer

C# RotateAround Half Circle Instead of Full Circle 0 Answers

How to rotate an object around another facing to mouse? 1 Answer

Draw single circle using RotateAround with key held down 1 Answer

Mouse Controlled object moving inside a circle. 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