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 Sipihr · Jul 16, 2021 at 12:11 PM · rigidbodyshakespinlocalspace

How to shake a spinning object?

Hello everyone, I have an object which I spin it by swipe input and I want to shake it when I've tapped. In my last attempt I could shake it but it doesn't carry the torque so it seems weird. Also I want it to get back in position smoothly, I have tried Lerp and Slerp but can't make it work. Any suggestions?

 public class PlatformController : MonoBehaviour
 {
     [SerializeField]
     private float rotationForce = 10f;
     private Vector3 firstTouch, lastTouch, touchDeltaPosition;
     private Touch touch;
     private bool shaking = false;
 
 
     void Update()
     {
         //If player touches the screen
         if(Input.touchCount > 0)
         {
             //Starting the touch pase
             touch = Input.GetTouch(0);
             switch (touch.phase)
             {
                 case TouchPhase.Began:
                     firstTouch = touch.position;
                     break;
                 case TouchPhase.Ended:
                     lastTouch = touch.position;
                     //Detecting if its tap or swipe
                     if(firstTouch == lastTouch)
                     {
                         //Calling the "not working" shake method
                         ShakeMe();
                         Debug.Log("Tapped");
                     }
                     if (firstTouch != lastTouch)
                     {
                         touchDeltaPosition = firstTouch - lastTouch;
                         GetComponent<Rigidbody>().AddRelativeTorque(0, touchDeltaPosition.x * rotationForce, 0);
                     }
                     break;
             }
         }
 
         if (shaking)
         {
             transform.rotation = Quaternion.Euler(-10, 0, 0);
         }
     }
 
     public void ShakeMe()
     {
         StartCoroutine("ShakeNow");
     }
 
     IEnumerator ShakeNow()
     {
         //Tried different things for return the cylinder to its original position but not succeded
 
         Quaternion originalRotation = transform.rotation;
 
         if (shaking == false)
         {
             shaking = true;
         }
 
         yield return new WaitForSeconds(0.2f);
 
         shaking = false;
         transform.rotation = originalRotation;
         //transform.rotation = Quaternion.Lerp(transform.rotation, originalRotation, Time.deltaTime * 0.1f);
     }
 }
 
Comment
Add comment · Show 2
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 Vivien_Lynn · Jul 16, 2021 at 04:51 PM 1
Share

Not the opportunity to look deeper into your code right now, but here's a suggestion how I would attempt this: I would create a parent Gameobject, which holds your rotating Gameobject. Then shake that parent gameobject. I would also write a seperate script for the shaking. This makes it easier to debug and makes it possible to use elsewhere as well.

Also, I notice that you use a string to call your Coroutine StartCoroutine("ShakeNow");. This is no good if you ever change the name of your Coroutine, then you lose the reference. Instead call it like a function, like so: StartCoroutine(ShakeNow());

avatar image Sipihr Vivien_Lynn · Jul 16, 2021 at 05:17 PM 0
Share

Really appreciate it, I will give it a shot and share the results. Also thanks for the tip. Cheers!

0 Replies

· Add your reply
  • Sort: 

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

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

Earth quake with physics? 1 Answer

Camera is shaking. Unity 3D 0 Answers

Shake Effect 0 Answers

Moving sphere tremble in moving camera view[Solved] 0 Answers

How to make a gamebject wiggle slowly as it moves forward ? 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