Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Kamuiyshirou · Feb 04, 2016 at 11:03 PM · rigidbody2dpluginrotatearound

Limit rotation and return the initial angle of an object

My code below works perfectly. The code rotates an object around a pivor . However while press the button " JUMP" it rotates endlessly . I need this to be limited. When the button is not pressed, the object must return to its original angle. As a pinball game .

alt text

 using UnityEngine;
 
 
 public class MoveBarra : MonoBehaviour {
     public Transform pivor;
     bool restoreRotation = false;
 
     void Update()
     {   
 
         Vector3 temp = pivor.transform.position;
 
         if (Input.GetAxis("Jump") == 1)
         {
             transform.RotateAround(temp, Vector3.forward, 500 * Time.deltaTime);
         }
 
 
         if (Input.GetAxis("Jump") == 0)
         {
             restoreRotation = true;
 
         }
         
 
         if (restoreRotation)
         {
 
             /* RETURN ROTATE HERE*/
         }
    
 
     }
 
 
 }


asds.png (322.2 kB)
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
1

Answer by aldonaletto · Feb 05, 2016 at 01:54 AM

Faking the flipper movement is somewhat easy - the big problem is how to simulate pinball physics, since the physics engine doesn't handle the flipper/ball collision correctly.
In order to fake the movement, you can store the initial rotation (and position) in member variables (variables declared outside any function) and measure the angle between the initial and current rotations with Quaternion.Angle, stopping when a maximum angle is reached.
Returning to the original position/rotation can be made by simply restoring the initial rotation and position (we need to restore rotation and position because RotateAround modifies both). That's an incorrect way to return the flipper to its initial position, since it returns immediately, but doesn't look so bad in practice.

 public class MoveBarra : MonoBehaviour {
      public Transform pivot;
      public float rotSpeed = -720.;
      public float maxAngle: = 90.;
     
      Quaternion rot0; 
      Vector3 pos0;
     
      void Start () {
         rot0 = transform.rotation; // save initial rotation...
         pos0 = transform.position; // and initial position
      }
     
      void Update () {
         // copy button state in a bool variable - Input.GetButton is somewhat slow
         bool button = Input.GetButton("Jump");
         if (button && Quaternion.Angle(transform.rotation, rot0) < maxAngle){
             // if button pressed and rotation not complete yet, rotate a little
             transform.RotateAround(pivot.position, pivot.up, rotSpeed * Time.deltaTime);
         }
         if (!button){ 
             // button released: return to initial position/rotation:
             transform.position = pos0;
             transform.rotation = rot0;
         }
     }
 }

Returning the flipper gradually is risky: Quaternion.Angle returns an absolute angle, thus the flipper may pass back the initial rotation and continue rotating forever like a crazy clock hand: the angle returned is absolute, thus it falls to zero and then starts growing again.
Anyway, the second if could be replaced by a code like this:

         if (!button && Quaternion.Angle(transform.rotation, rot0) > 3){
             // if button released and angle greater than 3 degrees, rotate back a little
             transform.RotateAround(pivot.position, pivot.up, -rotSpeed * Time.deltaTime);
         }
Comment
Add comment · 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 Kamuiyshirou · Feb 05, 2016 at 03:23 PM 0
Share

Perfect!!!

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

35 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

Related Questions

Is there a RotateAround functionality for Rigidbody 2D's? 1 Answer

How to move rotated knife(RigidBody2D) diagonally? 1 Answer

Hey, is there a way to have an object constantly keeping up with a mouse collision, as the mouse seems to fast for the update() 0 Answers

how would I change player.transform.RotateAround to MoveRotation 0 Answers

storing velocity and temporarly using it 0 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