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 /
avatar image
0
Question by BilboStabbins · Apr 19, 2017 at 12:52 AM · rotatearoundcirclekeypress

Draw single circle using RotateAround with key held down

Hi everyone,

I'm trying to draw a single circle using RotateAround by holding down a key. However, I'm having trouble detecting when the circle is complete. I'd like to stop the object moving when one circle has been drawn but not sure of the condition to use to check this. Any suggestions would be great! Thanks

Note, the reason for the line:

  m_startingAngle = 360 - m_startingAngle;

in Start() is so that the angle change is read as positive in the clockwise direction (-1) as well as a/clockwise (1). If there is a better way read the angles in both directions as positive from the starting angle then please suggest it.

Code is below:

 public class RotateAroundTest : MonoBehaviour
 {
     public delegate void KeyPressedDelegate();
     public static KeyPressedDelegate OnKeyPressedDelegateEvent;
 
     [SerializeField]
     private Transform m_pivotPoint;
 
     [SerializeField]
     private bool m_isCircleComplete = false;
 
     [SerializeField]
     private bool m_hasStarted = false;
 
     [SerializeField]
     private KeyCode m_key;
 
     [SerializeField]
     private int m_direction = 1;
 
     [SerializeField]
     private float m_speed = 1;
 
     [SerializeField]
     private float m_startingAngle = 0;
 
     [SerializeField]
     private float m_currentAngle = 0;
 
     private void Start()
     {
         m_startingAngle = transform.transform.rotation.z;
 
         if (m_direction == -1)
             m_startingAngle = 360 - m_startingAngle;
 
         OnKeyPressedDelegateEvent += DrawCircle;
     }
 
     private void Update()
     {
         if (Input.GetKey(m_key))
         {
             m_currentAngle = Mathf.Abs(transform.transform.eulerAngles.z - m_startingAngle);
 
             OnKeyPressedDelegateEvent();
         }
     }
 
     private void DrawCircle()
     {
         Debug.Log("transform.transform.rotation.z: " + transform.transform.eulerAngles.z);
         Debug.Log("m_startingAngle: " + m_startingAngle);
         Debug.Log("m_currentAngle: " + m_currentAngle);
 
         transform.RotateAround(m_pivotPoint.position, Vector3.forward, Time.deltaTime * m_speed * m_direction);
     }
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
0

Answer by HarshadK · Apr 19, 2017 at 05:56 AM

This is not exactly the best solution but it gets the job done. Check comments in the code for info on how it works.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class RotateAroundTest : MonoBehaviour
 {
     public delegate void KeyPressedDelegate();
     public static KeyPressedDelegate OnKeyPressedDelegateEvent;
 
     [SerializeField]
     private Transform m_pivotPoint;
 
     [SerializeField]
     private bool m_isCircleComplete = false;
 
     [SerializeField]
     private bool m_hasStarted = false;
 
     [SerializeField]
     private KeyCode m_key;
 
     [SerializeField]
     private int m_direction = 1;
 
     [SerializeField]
     private float m_speed = 1;
 
     [SerializeField]
     private float m_startingAngle = 0;
 
     [SerializeField]
     private float m_currentAngle = 0;
 
     // Anglular range between starting and current angle which will consider circle as complete.
     private float m_CircleCompleteRange = 2f;
 
     private void Start()
     {
         m_startingAngle = transform.transform.rotation.z;
 
         if (m_direction == -1)
             m_startingAngle = 360 - m_startingAngle;
 
         OnKeyPressedDelegateEvent += DrawCircle;
     }
 
     private void Update()
     {
         if (Input.GetKey(m_key))
         {
             // Update only if circle is not complete.
             if (!m_isCircleComplete) {
                 m_currentAngle = Mathf.Abs (transform.transform.eulerAngles.z - m_startingAngle);
 
                 OnKeyPressedDelegateEvent ();
             }
         }
     }
 
     private void DrawCircle()
     {
         Debug.Log("transform.transform.rotation.z: " + transform.transform.eulerAngles.z);
         Debug.Log("m_startingAngle: " + m_startingAngle);
         Debug.Log("m_currentAngle: " + m_currentAngle);
 
         // We wait for angle difference to be more than 10 degrees here so that we do not consider circle being complete when it starts rotating.
         if (Mathf.Abs (m_currentAngle - m_startingAngle) > 10) {
             m_hasStarted = true;
         }
 
         transform.RotateAround(m_pivotPoint.position, Vector3.forward, Time.deltaTime * m_speed * m_direction);
 
         // Circle completion check.
         if (m_hasStarted && Mathf.Abs(m_currentAngle - m_startingAngle) <= m_CircleCompleteRange) {
             m_isCircleComplete = true;
         }
     }
 }

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

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

66 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 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 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

Circling around a target while maintaining direction of facing 0 Answers

Rotation circle to stop at a certain angle 1 Answer

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

C# RotateAround Half Circle Instead of Full Circle 0 Answers

Rolling Cube with Rigidbody? 5 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