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 /
  • Help Room /
avatar image
0
Question by SaadAnees · Mar 31, 2016 at 09:59 AM · unity 5camerartscamera movement

How to move camera in x-z axis only for RTS touch script for mobile ?

Hello,

I am working on RTS based game for mobile. I want to control camera just like "Clash of Clan", I found a tutorial and script by Savalish on youtube. But that was for 2D game. I want that result in 3D so i modified it. Everything is working fine except the camera is moving upward direction that is y-axis. I don't want that. and also need boundaries for limiting camera to move further. Below is the code I used:

 using UnityEngine;
 using System.Collections;
 
 public class TouchCameraControl : MonoBehaviour 
 {
     public float moveSensitivityX = 1.0f;
     public float moveSensitivityY = 1.0f;
     public bool updateZoomSensitivity = true;
     public float orthoZoomSpeed = 0.05f;
     public float minZoom = 1.0f;
     public float maxZoom = 20.0f;
     public bool invertMoveX = false;
     public bool invertMoveY = false;
     public float mapWidth = 60.0f;
     public float mapHeight = 40.0f;
     
     public float inertiaDuration = 1.0f;
     
     private Camera _camera;
 
     private float minX, maxX, minY, maxY;
     private float horizontalExtent, verticalExtent;
     
     private float scrollVelocity = 0.0f;
     private float timeTouchPhaseEnded;
     private Vector3 scrollDirection = Vector3.zero;
 
     void Start () 
     {
         _camera = Camera.main;
 
         maxZoom = 0.5f * (mapWidth / _camera.aspect);
 
         if (mapWidth > mapHeight)
             maxZoom = 0.5f * mapHeight;
 
         if (_camera.fieldOfView > maxZoom)
             _camera.fieldOfView = maxZoom;
 
         CalculateLevelBounds ();
     }
     
     void Update () 
     {
         if (updateZoomSensitivity)
         {
             moveSensitivityX = _camera.fieldOfView / 5.0f;
             moveSensitivityY = _camera.fieldOfView / 5.0f;
         }
 
         Touch[] touches = Input.touches;
 
         if (touches.Length < 1)
         {
             //if the camera is currently scrolling
             if (scrollVelocity != 0.0f)
             {
                 //slow down over time
                 float t = (Time.time - timeTouchPhaseEnded) / inertiaDuration;
                 float frameVelocity = Mathf.Lerp (scrollVelocity, 0.0f, t);
                 _camera.transform.position += -(Vector3)scrollDirection.normalized * (frameVelocity * 0.05f) * Time.deltaTime;
                 
                 if (t >= 1.0f)
                     scrollVelocity = 0.0f;
             }
         }
 
         if (touches.Length > 0)
         {
             //Single touch (move)
             if (touches.Length == 1)
             {
                 if (touches[0].phase == TouchPhase.Began)
                 {
                     scrollVelocity = 0.0f;
                 }
                 else if (touches[0].phase == TouchPhase.Moved)
                 {
                     Vector3 delta = touches[0].deltaPosition;
 
                     float positionX = delta.x * moveSensitivityX * Time.deltaTime;
                     positionX = invertMoveX ? positionX : positionX * -1;
 
                     float positionY = delta.y * moveSensitivityY * Time.deltaTime;
                     positionY = invertMoveY ? positionY : positionY * -1;
 
                     _camera.transform.position += new Vector3 (positionX, 0, positionY);
                     //_camera.transform.position += transform.TransformDirection((Vector2)((new Vector2 (positionX, positionY-positionX))));
                     scrollDirection = touches[0].deltaPosition.normalized;
                     scrollVelocity = touches[0].deltaPosition.magnitude / touches[0].deltaTime;
 
 
                     if (scrollVelocity <= 100)
                         scrollVelocity = 0;
                 }
                 else if (touches[0].phase == TouchPhase.Ended)
                 {
                     timeTouchPhaseEnded = Time.time;
                 }
             }
 
 
             //Double touch (zoom)
             if (touches.Length == 2)
             {
                 Debug.Log ("Double Touch");
                 Vector2 cameraViewsize = new Vector2 (_camera.pixelWidth, _camera.pixelHeight);
 
                 Touch touchOne = touches[0];
                 Touch touchTwo = touches[1];
 
                 Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
                 Vector2 touchTwoPrevPos = touchTwo.position - touchTwo.deltaPosition;
 
                 float prevTouchDeltaMag = (touchOnePrevPos - touchTwoPrevPos).magnitude;
                 float touchDeltaMag = (touchOne.position - touchTwo.position).magnitude;
 
                 float deltaMagDiff = prevTouchDeltaMag - touchDeltaMag;
 
                 _camera.transform.position += _camera.transform.TransformDirection ((touchOnePrevPos + touchTwoPrevPos - cameraViewsize) * _camera.fieldOfView / cameraViewsize.y);
 
                 _camera.fieldOfView += deltaMagDiff * orthoZoomSpeed;
                 _camera.fieldOfView = Mathf.Clamp (_camera.fieldOfView, minZoom, maxZoom) - 0.001f;
 
                 _camera.transform.position -= _camera.transform.TransformDirection ((touchOne.position + touchTwo.position - cameraViewsize) * _camera.fieldOfView / cameraViewsize.y);
 
                 CalculateLevelBounds ();
             }
         }
     }
 
     void CalculateLevelBounds ()
     {
         verticalExtent = _camera.fieldOfView;
         horizontalExtent = _camera.fieldOfView * Screen.width / Screen.height;
         minX = horizontalExtent - mapWidth / 2.0f;
         maxX = mapWidth / 2.0f - horizontalExtent;
         minY = verticalExtent - mapHeight / 2.0f;
         maxY = mapHeight / 2.0f - verticalExtent;
     }
 
     void LateUpdate ()
     {
         //Vector3 limitedCameraPosition = _camera.transform.position;
         //limitedCameraPosition.x = Mathf.Clamp (limitedCameraPosition.x, minX, maxX);
         //limitedCameraPosition.y = Mathf.Clamp (limitedCameraPosition.y, minY, maxY);
         //_camera.transform.position = limitedCameraPosition;
     }
 
     void OnDrawGizmos ()
     {
         //Gizmos.DrawWireCube (Vector3.zero, new Vector3 (mapWidth, mapHeight, 0));
     }
 }

Tell me please what is wrong with the code. Any help would appreciated.

Thanks.

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

83 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

Related Questions

If Scene come to 20 second, first camera change other second camera 2 Answers

So I'm attempting to make a camera at a fixed angle with no rotation,I'm trying to set my camera on a follow path while keeping a fixed angle. 1 Answer

How to add a frame to my camera view in the game view? 1 Answer

Is my MonoDeveloped Glitchd ??? 1 Answer

Smooth Look at... 2 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