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 PijusVose · Jul 13, 2017 at 02:42 PM · camera3dtouchpan

Pan Orthographic camera in 3D

How do I pan a camera with touch(mouse drag also works), which is rotated on the X by 30 and on the Y by 45 degrees. I want the camera to move like in Clash of Clans or similar strategy games.

Here's what I have right now, but it doesn't work properly:

 public class CameraMovement : MonoBehaviour {
 
     public Transform camDrone;
     public Camera mainCam;
     public float camSpeed;
     
     // Update is called once per frame
     void Update () {
         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
             Vector2 touchPos = Input.GetTouch (0).deltaPosition;
             Vector3 camPos = new Vector3 (-touchPos.x, 10f, -touchPos.y);
             camDrone.Translate (camPos * camSpeed * Time.deltaTime);
         }
     }
 }
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

4 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by springwater · Jul 13, 2017 at 02:53 PM

I think, rather than translate, you will want to use lerp or Vector3. Lerp or whatever it's called and create way points as necessary. Just check out lerp the documentation.

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 PijusVose · Jul 13, 2017 at 02:55 PM 0
Share

The problem is, that my camera is at an angle. Doesn't matter if I use Lerp or Translate. I just need to know how I make camera movement with touch in a 3D game.

avatar image
-1

Answer by idontneedthat · Jul 13, 2017 at 03:17 PM

I think you should rewrte it and as your camera is rotated it will not work as you expect. I think you should rewrte it and as your camera is rotated it will not work as you expect.

Comment
Add comment · 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
-1

Answer by TWicked · Jul 14, 2017 at 08:17 AM

Simple solution would be to an empty game object and add camera to it as a child. When you rotate/move that game object, camera will move with it keeping intended orientation.

Comment
Add comment · 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
0

Answer by AtGfx · Jul 13, 2017 at 03:08 PM

I think that your code nearly works. Just specify Space.World as the second argument of Translate method. If you left it unset, the local basis of object is used, and as your camera is rotated it will not work as you expect.

Comment
Add comment · Show 2 · 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 PijusVose · Jul 13, 2017 at 03:15 PM 0
Share

This works, but whenever I touch again, it goes back to the camera initial position.

 void Update () {
         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.$$anonymous$$oved) {
             Touch touch = Input.GetTouch (0);
 
             // create a ray from the current touch point into the world
             Ray curPosRay  = Camera.main.ScreenPointToRay( touch.position );
 
             // fire that ray out and see if it hits anything 
             RaycastHit curHitInfo;
             if( Physics.Raycast( curPosRay, out curHitInfo ) )
             {
                 // we have a hit, do the same for the last position
                 Ray lastPosRay = Camera.main.ScreenPointToRay( lastPosition );
                 RaycastHit lastHitInfo;
                 if( Physics.Raycast( lastPosRay, out lastHitInfo ) )
                 {
                     // get the delta from those positions
                     Vector3 deltaPos = curHitInfo.point - lastHitInfo.point;
 
                     // zap y changes, we're only concerned about the x/z plane
                     deltaPos.y = 0;
 
                     // reverse the direction to negate the movement such that the touch point
                     // stays over the same spot once the camera moves.
                     deltaPos *= -1;
 
                     // apply the pan
                     camDrone.Translate(deltaPos * Time.deltaTime * camSpeed);
                 }
             }
             lastPosition = touch.position;
         }
     }
avatar image AtGfx PijusVose · Jul 14, 2017 at 06:42 AM 0
Share

$$anonymous$$aybe you should record a vule in lastPosition when user touch the screen, then execute your function when user move his finger. Otherwise when you take your finger off the screen you have the lastPosition recorded and when you move again in another spot, the first execution of your method will use the last position recorded for the last move on the touch screen.

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

128 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

Related Questions

pan camera around with buttons or touch 0 Answers

Rotate, pan and zoom camera lerp touch 0 Answers

pan camera by touch / gui button 0 Answers

Force 3D touch IOS 1 Answer

How to use two cameras in VR to display 3D object on top of UI panel? 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