• 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 aman_jha · Jul 20, 2014 at 03:31 PM · c#rotationtransformrotatearound

RotateAround Limitations

Hey Unitarians!

In my game, I have a block called a swing block. The block part of it attaches to the ceiling of the game and the cord that is attached to it rotates constantly. It moves back and forth like a never-ending pendulum. At least, that's what it should do.

What I have right now is that the cord of the swing rotates around the swing block, as it should, but it keeps going all 360 degrees - making a circle. This is probably because it is in the update function. I want it so that after 30 degrees of moving to the left, it switches direction and moves to the right, until it reaches 30 degrees in that direction, and switches back. How would I do this?

Currently my script: using UnityEngine; using System.Collections;

 public class SwingBlock : MonoBehaviour {
 
     public float RotateSpeed = 100;
     public float angleMax = 30.0f;
 
     private Transform SwingRoot;
     //private Transform EndRoot;
     
 
     void Start () {
         SwingRoot = this.transform.parent.gameObject.transform;
     }
     
     void Update () {
         //Rotation controller
         transform.RotateAround (SwingRoot.position, Vector3.forward, RotateSpeed * Time.deltaTime);
     }
 }
 
Comment

People who like this

0 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

· Add your reply
  • Sort: 
avatar image

Answer by Tehnique · Jul 20, 2014 at 03:48 PM

I think the best way to go is to use a Hinge Joint. Some examples: EX1, EX2.

If that is not possible, for any reason, you'll have to script your limits. Take a look here.

Comment

People who like this

0 Show 1 · 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 aman_jha · Jul 22, 2014 at 01:45 PM 0
Share

I'm not really getting the Hinge Joint, and sicne my game is made for mobile, I think if I have a lot of them they might affect performance. I'll try scripting the limits

avatar image

Answer by AndyMartin458 · Jul 21, 2014 at 05:12 PM

When your pendulum gets to the proper angles, use -1.0f * Vector3.forward as the rotation angle. Store the original Vector3 and determine the present angle . Something like this.

 void Start()
 {
     Vector3 origAngle = SwingRoot.position;
 }
 
 void Update()
 {
     float curAngle = Vector3.angle(SwingRoot.position, origAngle);
 
     Vector3 rotAngle;
     if(curAngle >= angleMax)
     {
         rotAngle = -1.0f * Vector3.forward;
     }
     else
     {
         rotAngle = Vector3.forward;
     }
 
     transform.RotateAround (SwingRoot.position, rotAngle, RotateSpeed * 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 aman_jha · Jul 22, 2014 at 01:48 PM 0
Share

I'm trying your script, but I'm not seeing anything different. I modified one line of your code where it said Vector3.angle, because Unity said it was Vector3.Angle.

avatar image aman_jha · Jul 22, 2014 at 01:48 PM 0
Share
 using UnityEngine;
 using System.Collections;
 
 public class SwingBlock : MonoBehaviour {
 
     public float RotateSpeed = 100;
     public float angleMax = 30.0f;
 
     private Transform SwingRoot;
     //private Transform EndRoot;
 
     private Vector3 origAngle;
     private float curAngle;
     
 
     void Start () {
         SwingRoot = this.transform.parent.gameObject.transform;
         Vector3 origAngle = SwingRoot.position;
     }
     
     void Update () {
         //Rotation controller
         //transform.RotateAround (SwingRoot.position, Vector3.forward, RotateSpeed * Time.deltaTime);
         float curAngle = Vector3.Angle(SwingRoot.position, origAngle);
         
         Vector3 rotAngle;
         if(curAngle >= angleMax){
             rotAngle = -1.0f * Vector3.forward;
         }
 
         else{
             rotAngle = Vector3.forward;
         }
         
         transform.RotateAround (SwingRoot.position, rotAngle, RotateSpeed * Time.deltaTime);
     }
 }
 

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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

24 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

Related Questions

C# Need Help Refining Ricochet Script 1 Answer

Rotate object around object with fixed value. 1 Answer

Flip over an object (smooth transition) 3 Answers

Rotate around moves object out of position, why? 1 Answer

How to rotate on one axis while keeping the other axes open to be rotated by other scripts? 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