• 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 zombiehaha · Aug 09, 2012 at 12:01 PM · cameradrag

Drag to Rotate/Scroll a Camera

I'm trying to setup a rotating/scrolling camera (position is locked) where the movement matches a finger drag (like PULLING the scene into focus).

Everything appears to work, except that I'm finding that when the drag starts the camera will always initially snap back before performing the proper drag/scroll action.

I presume this isn't functioning correctly due to the Slerp argument (or maybe even the ScreenWorldToPoint) , but I'm not quite certain how to resolve it. Any thoughts or suggestions (or even a better method) would be appreciative.

here is my code...Thank you.

 public float damping = 5f; 
 private Vector3 targetPosition; // screen coordinate of touch (x,y) converted to world via 'camera.ScreenToWorldPoint(fingerPosition.x, fingerPosition.y, camera.nearClipPlane)'
 // this value is automatically updated as long as the user is dragging 
 
 void Update()
 {
     Quaternion rotation = Quaternion.Inverse(Quaternion.LookRotation(targetPos - transform.position)); 
     tranform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping); 
     transform.forward = new Vector3(transform.forward.x, 0, transform.forward.z); // limit rotation along Y
 }
 
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
0

Answer by robotmaker01 · Aug 09, 2012 at 02:19 PM

i dont know if this what you want, or does this work, but heres my idea.there is a "mouse look" script in:

Standard\\Assets\\Character Controllers\\Sources\\Scripts u can change that code to only work when the mouse button is down, and then use that script on the camera u want.

i think that this is what u want.

copy paste this to mouse look:

 // C#
 // MouseLook.cs
 using UnityEngine;
 using System.Collections;
 
 [AddComponentMenu("Camera-Control/Mouse Look")]
 public class MouseLook : MonoBehaviour
 {
     public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
     public RotationAxes axes = RotationAxes.MouseXAndY;
     public float sensitivityX = 15F;
     public float sensitivityY = 15F;
     
     public float minimumX = -360F;
     public float maximumX = 360F;
     
     public float minimumY = -60F;
     public float maximumY = 60F;
     public float h = -180;
     
     float rotationY = 0F;
     
     void Update ()
     {
         if(Input.GetMouseButton(0))
         {
             if (axes == RotationAxes.MouseXAndY)
             {
                 float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
                 rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
                 rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
                 transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
             }
             else
             {
                 rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
                 rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
                 transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
             }
         }
     }
     
     void Start ()
     {
         transform.Rotate(h,0,0);
         // Make the rigid body not change rotation
         if (rigidbody)
             rigidbody.freezeRotation = true;
     }
 }


EDIT:negetive the X and Y to get the drag like.

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 zombiehaha · Aug 10, 2012 at 02:59 AM 0
Share

thanks for the comment, i had a look at $$anonymous$$ouseLook.cs. Actually currently I get the same results using my present code (sans Quaternion.Inverse). I'm thinking it may have something to do with the touch class I'm using, but unfortunately I'm not at liberty to divulge that.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

The solution with camera rotation which is used in most strategic games 1 Answer

Rotating camera with RMB drag 1 Answer

How to have Camera following a Target, but not directly centered? 3 Answers

Drag camera with two finger swipe 1 Answer

Mouse/Wacom stylus drag not working. 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