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
1
Question by Sawula · Dec 31, 2014 at 01:37 PM · c#rotatestoparound

stop rotate around c#

Hey guys, I got a little problem.

I have an enemy gameobject that I want to rotate around a background gameobject on a specific angle I tried this bit of code

 using UnityEngine;
 using System.Collections;
 
 public class rotatearoundme : MonoBehaviour {
     public float rotationSpeed = 5f;
     
     private float currentYAngle = 0f;
     
     private float targetYAngle = 0f;
     
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update() {
         if (Input.GetKey(KeyCode.Alpha1))
             targetYAngle = 0 - 90;
         else if (Input.GetKey(KeyCode.Alpha2))
             targetYAngle = 90 - 90;
         else if (Input.GetKey(KeyCode.Alpha3))
             targetYAngle = 180 - 90;
         else if (Input.GetKey(KeyCode.Alpha4))
             targetYAngle = 270 - 90;
           
         currentYAngle = Mathf.MoveTowardsAngle(transform.eulerAngles.y, targetYAngle, rotationSpeed * Time.deltaTime);
         transform.RotateAround(new Vector3(0f,6.73f,30f), Vector3.up, currentYAngle);
              }
 }


But it doesn't stop the rotation. The enemy just keep orbiting around the cube instead of stopping. What did I miss?

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
Best Answer

Answer by RudyTheDev · Dec 31, 2014 at 03:00 PM

A couple notes first:

Firstly, you cannot use transform.eulerAngles.y as continuous input in place of currentYAngle because of gimbal lock. At certain angles, the eulerAngles can snap/change, while currentYAngle remains as it was. You can search for this "issue" around. Generally, if you rotate something yourself using euler angles, you should keep the "real" value yourself. Otherwise rotate via quaternions. (Mathf.MoveTowardsAngle has a bad example.)

Secondly, Transform.RotateAround()'s third parameter is the amount to rotate. It is not the angle that it should arrive at. Using currentYAngle (or transform.eulerAngles.y) won't provide any sane results, because that is the object's own rotation, and has no connection to this new Vector3(0f,6.73f,30f) point.

Here is a solution with some trigonometry:

 using UnityEngine;
 using System.Collections;
 
 public class rotatearoundme : MonoBehaviour
 {
     public float rotationSpeed = 360f;
 
     public float distance = 5f;
 
     public Vector3 pivot = new Vector3(0f, 6.73f, 30f);
 
     private float currentYAngle = 0f;
 
     private float targetYAngle = 0f;
 
     // Use this for initialization
     void Start()
     {
     }
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKey(KeyCode.Alpha1))
             targetYAngle = -90;
         else if (Input.GetKey(KeyCode.Alpha2))
             targetYAngle = 0;
         else if (Input.GetKey(KeyCode.Alpha3))
             targetYAngle = 90;
         else if (Input.GetKey(KeyCode.Alpha4))
             targetYAngle = 180;
 
         currentYAngle = Mathf.MoveTowardsAngle(currentYAngle, targetYAngle, rotationSpeed * Time.deltaTime);
 
         transform.position = new Vector3(
             pivot.x + Mathf.Sin(currentYAngle * Mathf.Deg2Rad) * distance,
             transform.position.z,
             pivot.z + Mathf.Cos(currentYAngle * Mathf.Deg2Rad) * distance
         );
     }
 }

Here you would treat currentYAngle/targetYAngle as the angle towards the pivot (and not the object's rotation, although it would be very similar if it had to face the pivot).

Mathf.Sin(currentYAngle * Mathf.Deg2Rad) * distance, is

alt text

where R is distance and theta is currentYAngle.


sin_cos_01.gif (3.9 kB)
Comment
Add comment · 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 Sawula · Dec 31, 2014 at 03:14 PM 0
Share

Lots of $$anonymous$$ath, but it does the trick, amazing thanks!

avatar image RudyTheDev · Dec 31, 2014 at 04:57 PM 0
Share

You'd be surprised how many angle/location problems can be solved with x=r*cos(a) y=r*sin(a) . All you need is angle and distance. ;)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Move around object 1 Answer

Mixing mecanim movement with script rotation problem 1 Answer

Smooth transition from 360 to 0 degrees 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