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 raymond chin · Jun 30, 2014 at 09:55 PM · quaterniontransform.rotatetransform.rotatearound

Drag to Rotate using Arcball theory ( 3 axis )

Im trying to rotate a 3D object using the arcball theory, so far i've found one that does what i wanted, BUT it uses transform.rotateAround that constantly applies angular velocity everytime i swipe (still holding down my fingers), what i wanted is to use transform.Rotate , and i want to be able to "Drag to Rotate" the object. meaning if i dont move my finger, the object should'nt move.

I tweaked the code so i get what i wanted, but even i know that it isn't efficient or even effective.

What i did = i added a variable delta, which is the delta of my fingers location every frame. Meaning it can calculate how fast im moving my fingers. Then i applied the delta on the angularvelocity, so when my finger moves faster the object rotates faster, but when i stop moving my fingers, the object angularvelocity is 0.

i know the code is very barbaric but i really cant figure out any other way, maybe it's because i dont have deep comprehension on quaternions and other function relating to this purpose. ive spent days trying to solve this, maybe someone can shed some light to this question using transform.Rotate ? much appreciated.

 using UnityEngine;
 using System.Collections;
 
 public class Rotate_Swipe : MonoBehaviour {
     
     private float damping = 0.9f;
     private static float speed = 2;
 
     private Vector3 delta = Vector3.zero;
     private Vector3 lastPos = Vector3.zero;
     private Vector3 vDown;
     private Vector3 vDrag;
     private bool dragging;
     private float angularVelocity;
     private Vector3 rotationAxis;

     void Start ()
     {
         dragging = false;
         angularVelocity = 0;
         rotationAxis = Vector3.zero;
     }
     
     void Update ()
     {    

             if (Input.touchCount == 1) {
                 Ray ray = Camera.main.ScreenPointToRay (Input.GetTouch(0).position);
                 RaycastHit hit;
 
                 if (Physics.Raycast (ray, out hit)) {
                     if (!dragging) {
                         //extract vDown from the RaycastHit
                         vDown = hit.point - transform.position;
                         dragging = true;
                     } else {
                         // extract vDrag from the RaycastHit
                         delta = hit.point - lastPos;
                         vDrag = hit.point - transform.position;
                         
                         // compute the rotation axis and angular velocity from vDown and vDrag
                         rotationAxis = Vector3.Cross (vDown, vDrag);
                         angularVelocity = Vector3.Angle (vDown, vDrag) * (Mathf.Abs(delta.magnitude)+speed);
                         vDown = hit.point - transform.position;
                         lastPos = hit.point;
                         
                     }

                 else
                     dragging = false;
             }
         }
 
             if (Input.GetMouseButtonUp (0) || Input.touchCount != 1)
                 dragging = false;
             
             // apply the angular velocity
                 transform.RotateAround (rotationAxis, angularVelocity * Time.deltaTime);
                 angularVelocity = (angularVelocity > 0.01f) ? angularVelocity * damping : 0;
 
 
     }

 



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 JefferyWright · Jan 10, 2016 at 06:46 AM 0
Share

Does this code work? I got two errors right off the bat:

 Assets/ArcBall.cs.cs(46,36): error CS1525: Unexpected symbol `else'

and:

    Assets/ArcBall.cs.cs(61,1): error CS8025: Parsing error
avatar image Scribe JefferyWright · Jan 10, 2016 at 05:25 PM 0
Share

It's missing a second curly brace before the else on line 46 I believe.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Transform.rotation vs Wrapping 1 Answer

RotateAround object in local space 1 Answer

Rotate Issue 0 Answers

Rotating game Object. 2 Answers

Rotate Object Around Local X Axis , then Local Y Axis, then Local Z Axis (like a turtle) 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