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 3dexplorer · Jun 19, 2015 at 07:59 AM · rotationobjecttouch

Control over y-axis limit in rotating object by touch

Hello,I am 3d artist but less into scripts.Need help to resolve my issue.Found script for rotating object by touch but my problem is i need to control over y-axis limit in down direction(require upper- hemisphere rotation)only. below is the code:


using UnityEngine; using System;

 public class TransformObject
 {
     ///////////////////////////////////////////////////////////////////////////
     #region Variables
 
     // variables
 
 #if UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_WEBPLAYER
     private float RotationSpeed = 1500;
     private float MoveSpeed = 10.0f;
     private float ZoomSpeed = 15.3f;
 #endif // UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_WEBPLAYER
 
 #if UNITY_ANDROID || UNITY_IPHONE
     private float RotationSpeed = 15f;
     private float MoveSpeed = 1.09f;
     private float ZoomSpeed = 0.009f;
 
     private float mOldFingerDelta = 0;
     private const float mFingerDistanceEpsilon = 1.0f;
 
 #endif // UNITY_ANDROID || UNITY_IPHONE
 
     public float MinDist = 2.0f;
     public float MaxDist = 50.0f;
 
     private Transform mMoveObject = null;
 
     #endregion
     ///////////////////////////////////////////////////////////////////////////
 
     ///////////////////////////////////////////////////////////////////////////
     #region Public methods
 
     /// <summary>
     /// 
     /// </summary>
     public TransformObject()
     {
         EnabledMoving = true;
     }
 
     /// <summary>
     /// Sets transform that will be used as "center" of the rotate / pan / zoom
     /// </summary>
     public void SetTransformRotateAround(Transform goMove)
     {
         mMoveObject = goMove;
         if (mMoveObject == null)
         {
             Debug.LogWarning("Error! Cannot find object!");
             return;
         }
     }
 
     public void Update()
     {
         if (!EnabledMoving)
         {
             return;
         }
 
         Vector3 dir = mMoveObject.position - Camera.main.transform.position;
         float dist = Math.Abs(dir.magnitude);
 
         Vector3 camDir = Camera.main.transform.forward;
         Vector3 camLeft = Vector3.Cross(camDir, Vector3.down);
         Vector3 camDown = Vector3.Cross(camDir, camLeft);
         //Vector3 camUp = Vector3.Cross(camLeft, camDir);
 
 #if UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_WEBPLAYER
 
         float dx = Input.GetAxis("Mouse X");
         float dy = Input.GetAxis("Mouse Y");
 
         // rotate
         if (Input.GetMouseButton(0))
         {
             mMoveObject.Rotate(camLeft, dy * RotationSpeed * Time.deltaTime, Space.World);
             mMoveObject.Rotate(Vector3.down, dx * RotationSpeed * Time.deltaTime, Space.Self);
         }
 
         // move
         if (Input.GetMouseButton(1))
         {
             Vector3 camPos = Camera.main.transform.position;
             camPos += -camLeft * MoveSpeed * dx * Time.deltaTime;
             camPos += -camDown * MoveSpeed * dy * Time.deltaTime;
             Camera.main.transform.position = camPos;
         }
 
         // zoom
         if (Input.GetAxis("Mouse ScrollWheel") > 0)
         {
             if (dist > MinDist)
             {
                 mMoveObject.Translate(-dir * ZoomSpeed * Time.deltaTime, Space.World);
             }
         }
 
         if (Input.GetAxis("Mouse ScrollWheel") < 0)
         {
             if (dist < MaxDist)
             {
                 mMoveObject.Translate(dir * ZoomSpeed * Time.deltaTime, Space.World);
             }
         }
 
 #endif // UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_WEBPLAYER
 
 #if UNITY_ANDROID || UNITY_IPHONE
 
         // rotate
         if (Input.touchCount == 1)
         {
             mMoveObject.Rotate(camLeft, Input.touches[0].deltaPosition.y * RotationSpeed * Time.deltaTime, Space.World);
             mMoveObject.Rotate(Vector3.down, Input.touches[0].deltaPosition.x * RotationSpeed * Time.deltaTime, Space.Self);
         }
 
         if (Input.touchCount == 2)
         {
             UnityEngine.Vector2 deltaFingerVec = Input.touches[0].position - Input.touches[1].position;
 
             float deltaFingerValue =
                 Mathf.Sqrt(deltaFingerVec.x * deltaFingerVec.x + deltaFingerVec.y * deltaFingerVec.y);
 
             // move
             bool moved = false;
             {
                 float moveX = 0;
                 float moveY = 0;
 
                 // moveX
                 if (Input.touches[0].deltaPosition.x < 0 && Input.touches[1].deltaPosition.x < 0)
                 {
                     moveX = Mathf.Max(Input.touches[0].deltaPosition.x, Input.touches[1].deltaPosition.x);
                     moved = true;
                 }
                 else if (Input.touches[0].deltaPosition.x > 0 && Input.touches[1].deltaPosition.x > 0)
                 {
                     moveX = Mathf.Min(Input.touches[0].deltaPosition.x, Input.touches[1].deltaPosition.x);
                     moved = true;
                 }
 
                 // moveY 
                 if (Input.touches[0].deltaPosition.y < 0 && Input.touches[1].deltaPosition.y < 0)
                 {
                     moveY = Mathf.Max(Input.touches[0].deltaPosition.y, Input.touches[1].deltaPosition.y);
                     moved = true;
                 }
                 else if (Input.touches[0].deltaPosition.y > 0 && Input.touches[1].deltaPosition.y > 0)
                 {
                     moveY = Mathf.Min(Input.touches[0].deltaPosition.y, Input.touches[1].deltaPosition.y);
                     moved = true;
                 }
 
                 Vector3 camPos = Camera.main.transform.position;
                 camPos += -camLeft * MoveSpeed * moveX * Time.deltaTime;
                 camPos += -camDown * MoveSpeed * moveY * Time.deltaTime;
                 Camera.main.transform.position = camPos;
             }
 
             // zoom
             if (!moved && Mathf.Abs(deltaFingerValue - mOldFingerDelta) > mFingerDistanceEpsilon)
             {
                 if (deltaFingerValue - mOldFingerDelta > 0)
                 {
                     if (dist > MinDist)
                     {
                         mMoveObject.transform.Translate(-dir * ZoomSpeed * Time.deltaTime * deltaFingerValue, Space.World);
                     }
                 }
 
                 if (deltaFingerValue - mOldFingerDelta < 0)
                 {
                     if (dist < MaxDist)
                     {
                         mMoveObject.transform.Translate(dir * ZoomSpeed * Time.deltaTime * deltaFingerValue, Space.World);
                     }
                 }
             }
 
             mOldFingerDelta = deltaFingerValue;
         }
 
 
 #endif // UNITY_ANDROID || UNITY_IPHONE
 
 
 
     }
     #endregion
     ///////////////////////////////////////////////////////////////////////////
 
     ///////////////////////////////////////////////////////////////////////////
     #region Properties
     /// <summary>
     /// Gets or set value indicating if transformation is enabled
     /// </summary>
     public bool EnabledMoving
     {
         get;
         set;
     }
 
     /// <summary>
     /// Gets game object that moves around
     /// </summary>
     public Transform MoveObject
     {
         get
         {
             return mMoveObject;
         }
     }
 
     #endregion
     ///////////////////////////////////////////////////////////////////////////
 }
 
 
 
 
 
 
 




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

2 People are following this question.

avatar image avatar image

Related Questions

how to set rotation / position of an object on trigger? 2 Answers

Touch mouseorbit 5 Answers

Setting rotation when instatiating a object 1 Answer

Different speed on different phones 0 Answers

how can i limit rotation on x and y 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