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 ce3bernardo · Mar 24, 2017 at 11:39 AM · c#rotationmobile

Help with refining rotation deciphering the switch and drag. This is for touch and C#.

Hey All!

First post here. I am creating a jar object and just like in real life you can rotate /spin the jar with your hands and stop it in place. This code works but I am having trouble making it look fluid and nice. I can clearly see the the switch between running switch and drag because it has a very hard stop once the distance was far enough. Any sense of direction or approach is greatly appreciated! I am a sponge and want to learn everything! Here is my pseudocode:

Pseudo

FixedUpdate()

  • addTorque to the rotation

For each touch:

TouchDown

  • Freeze the rotation. // when the finger is on the jar object, the object should stop rotating.

  • Initialize the touch. // needs to referenced to measure swipe.

TouchMove

  • if (swipe distance is greater than minimum swipe) // If swipe distance is greater than minimum swipe which is currently a test number. -----> then it is considered a swipe. Jar will rotate + the addTorque and to slow down rotation.

  • else ------> track the finger position and direction to drag and rotate the object.

TouchUp

  • Un-freeze the rotation

  • The touch from TouchDown is now a new Touch()

    My Actual Code

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class ButtonCube : MonoBehaviour {

 private Touch initialTouch = new Touch();
 private float rotationRate = 5.0f;
 private float amount = 5.0f;
 private float torque = 3.0f;
 public Rigidbody jar;
 private float getXAxis;
 private float distance;
 private bool hasSwiped = false;
 public Vector3 eulerAngleVelocity;
     
  void FixedUpdate() {
     if (Input.touchCount > 0) {
         getXAxis = Input.touches[0].deltaPosition.x;
     }
     float h = getXAxis * amount * Time.deltaTime;
     jar.AddTorque(transform.up * torque * -h);

     foreach (Touch touch in Input.touches) {
         Debug.Log ("Touching at: " + touch.position);
         if (touch.phase == TouchPhase.Began) {
             Debug.Log ("Freeze");
             jar.freezeRotation = true;
             initialTouch = touch;
         } 
         else if (touch.phase == TouchPhase.Moved && !hasSwiped) {
             Debug.Log ("Touch phase Moved");
             float deltaX = initialTouch.position.x - touch.position.x;
             distance = Mathf.Sqrt(Mathf.Pow(deltaX, 2));
             if (distance > 50f) { // this is where I need HELP the most!
                 Quaternion deltaRotation = Quaternion.Euler (eulerAngleVelocity * Time.deltaTime);
                 jar.MoveRotation (jar.rotation * deltaRotation);
                 hasSwiped = true;
             } else {
                 jar.transform.Rotate (0 * rotationRate, -touch.deltaPosition.x * rotationRate, 0, Space.World);
             }

             }
         else if (touch.phase == TouchPhase.Ended) {
             Debug.Log ("Touch phase Ended" + "UnFreeze"); 
             jar.freezeRotation = false;
             initialTouch = new Touch();
             hasSwiped = false;
         }
     }
 }        

}

Thank you in advance! I am excited to hear your feedback.

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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Flip over an object (smooth transition) 3 Answers

Get Accelerometer to rotate 360 degrees 2 Answers

Distribute terrain in zones 3 Answers

GO will not rotate on swipe. 0 Answers

Turning touch to rotation 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