Search Unity

3D Android Camera Script

Discussion in 'Scripting' started by AliquidHacker, Jun 7, 2015.

  1. AliquidHacker

    AliquidHacker

    Joined:
    Jan 10, 2015
    Posts:
    25
    Hey forum,
    Lately, I have been trying to make a 3D RealTimeStrategy type camera for a project I am working on,
    I am trying to make this on the android platform as this is something I have never done before.
    Every time I look up how do something like this it always comes up with "Clash of clans like the camera".
    But as most of you probably know clash of clans is not 3D but isometric (2.5D ish...).
    And so I was wondering if any of you could help me but a basic script like clash of clans, but for 3D. This is just because I do not fully understand android and iPhone yet.
    Thanks in advance,
    Aliquidhacker
     
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    I didn't find anything Clash of clans when I searched for
    "unity3d rts camera"

    The first hit gave me this post.
    Hope that helps.
     
  3. AliquidHacker

    AliquidHacker

    Joined:
    Jan 10, 2015
    Posts:
    25
    @Chris Trueman Thanks for the reply but as I mentioned in the post,
    I am not looking at how to make an rts camera but how to make one for android,
    Again thanks for the reply,
    AliquidHacker
     
  4. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    Its only a mater of changes in the input of a system like for it to work on Android.

    Instead of the arrow keys on the keyboard to move the camera around, you could use buttons on the screen.
    How the input works is really up to you and how you want the game to function.
     
  5. AliquidHacker

    AliquidHacker

    Joined:
    Jan 10, 2015
    Posts:
    25
    @Chris Trueman Sorry I have been away for a while,
    Thanks for the reply but I was looking like a dragging to move,
    Here is the small part of my camera code (C#) I feels its organised but that might just be me,

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraController : MonoBehaviour {
    5.     public float MovementSpeed = 5;
    6.  
    7.     void Update () {
    8.         if ( Input.GetKey("w")) {          
    9.             transform.Translate(
    10.                 Vector3.forward * Time.deltaTime * MovementSpeed);          
    11.         }
    12.         else if ( Input.GetKey("s")) {      
    13.             transform.Translate(
    14.                 Vector3.forward * Time.deltaTime * -MovementSpeed);          
    15.         }
    16.      
    17.         if ( Input.GetKey("d")) {          
    18.             transform.Translate(
    19.                 Vector3.right * Time.deltaTime * MovementSpeed);
    20.         }
    21.         else if ( Input.GetKey("a")) {          
    22.             transform.Translate(
    23.                 Vector3.right * Time.deltaTime * -MovementSpeed);            
    24.         }
    25.     }
    26. }
    How exactly would I make it sense the dragging on the screen,
    Thanks in advance,
    Aliquidhacker
     
    Last edited: Jun 9, 2015
  6. Aldo

    Aldo

    Joined:
    Aug 10, 2012
    Posts:
    173
    What I do is make 2 raycasts as soon as the level is loaded a 0,0 (screen) and at (width, height)

    Then you know how many game meters per screen pixel are there, so when you do the drag you multiply the X,Y drag per your pixel meter vector.
     
  7. AliquidHacker

    AliquidHacker

    Joined:
    Jan 10, 2015
    Posts:
    25
    @Aldo Any chance for a script example,
    Sorry im not too good with android scripting,
    Anyone else is welcome too share as well
    Aliquidhacker
     
  8. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    This is a very basic script, but it gets the job done with no raycasting. If you want elasticity you need to figure that out.

    Not tested on Android, but I don't see why it wouldn't work.
    Update. Needed to make some changes for it to work on Android properly.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. [RequireComponent(typeof(Camera))]
    6. public class TouchCamera : MonoBehaviour
    7. {
    8.    private Camera _cam;
    9.    private Vector3 _lastPosition = Vector3.zero;
    10.    private float _cameraToWorldRatio = 0.0f;
    11.  
    12.    void Awake()
    13.    {
    14.        _cam = GetComponent<Camera>();//Get the camera component.
    15.    }
    16.  
    17.    void Start()
    18.    {
    19.        //Calculate the current camera to world ratio to convert the camera's movements into word units.
    20.        Vector3 left = _cam.ScreenToWorldPoint(Vector3.zero);
    21.        Vector3 right = _cam.ScreenToWorldPoint(new Vector3(Screen.width, 0, 0));
    22.        float width = Vector3.Distance(left, right);
    23.        _cameraToWorldRatio = width / Screen.width;
    24.    }
    25.  
    26.    void Update()
    27.    {
    28.        if (Input.touchCount > 0)
    29.        {
    30.             //Get touch info
    31.            Touch touch = Input.GetTouch(0);//Get the touch info of the first finger.
    32.            Vector3 touchPos = new Vector3(touch.position.x, touch.position.y, 0f);
    33.  
    34.            if (touch.phase == TouchPhase.Moved)
    35.           {
    36.                Vector3 moveDelta = (_lastPosition - touchPos) * _cameraToWorldRatio;
    37.                transform.Translate(moveDelta);
    38.           }
    39.  
    40.           _lastPosition = touchPos;
    41.        }
    42.        else if (Input.mousePresent)//Might as well add mouse support.
    43.        {
    44.            if (Input.GetMouseButton(0))
    45.           {
    46.               Vector3 moveDelta = (_lastPosition - Input.mousePosition) * _cameraToWorldRatio;
    47.               transform.Translate(moveDelta);
    48.           }
    49.  
    50.           _lastPosition = Input.mousePosition;
    51.       }
    52.   }
    53. }
     
    Last edited: Jun 10, 2015
    Aldo and AliquidHacker like this.
  9. AliquidHacker

    AliquidHacker

    Joined:
    Jan 10, 2015
    Posts:
    25
    @Chris Trueman Thanks a lot,
    I made some changes for the things that weren't very necessary,
    I will post my whole script for anyone that needs it as soon as I am done,
    Thanks again,
    AliquidHacker
     
  10. Aldo

    Aldo

    Joined:
    Aug 10, 2012
    Posts:
    173
    @Chris Trueman ScreenToWorldPoint() is what I was calling as raycast, I would only change it to be bottom right instead of right by adding the height since the script might need Y movement too.

    And that script is basically the same that I would had given.
     
  11. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    ScreenToWorldPoint is some matrix math with no raycasting envolved.

    You don't need the Y component as you only need the ratio from screen to world. If you change it to use height instead of width you get the same number, so the script works in all directions.
     
  12. AliquidHacker

    AliquidHacker

    Joined:
    Jan 10, 2015
    Posts:
    25
    @Chris Trueman
    Nice little debate going on here ;)
    Anyway here is the script I modified,
    I also added a zoom function, but I do not think it is the most effective one,
    Anyone have an idea on how I could improve it?
    Unless it is perfect already (Doubt it ;)
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraController : MonoBehaviour {
    5.     private float _MovementSpeed = 0.005f;
    6.     private Vector3 _lastPosMove = Vector3.zero;
    7.  
    8.     void Update() {
    9.         if (Input.touchCount == 1) {
    10.  
    11.             Touch touch = Input.GetTouch(0);
    12.             Vector3 touchPos = new Vector3(touch.position.x, touch.position.y, 0f);
    13.          
    14.             if (touch.phase == TouchPhase.Moved) {
    15.                 Vector3 moveDelta = (_lastPosMove - touchPos) * _MovementSpeed;
    16.                 transform.Translate(moveDelta);
    17.             }
    18.          
    19.             _lastPosMove = touchPos;
    20.         }
    21.  
    22.         if(Input.touchCount == 2) {
    23.             Touch zoomOne = Input.GetTouch(0);
    24.             Touch zoomTwo = Input.GetTouch(1);
    25.  
    26.             Vector2 ZoomOnePrev = zoomOne.position - zoomOne.deltaPosition;
    27.             Vector2 ZoomTwoPrev = zoomTwo.position - zoomTwo.deltaPosition;
    28.  
    29.             float prevTouchDeltaMag = (ZoomOnePrev - ZoomTwoPrev).magnitude;
    30.             float touchDeltaMag = (zoomOne.position - zoomTwo.position).magnitude;
    31.  
    32.             float deltaMagnitudeDiff = (prevTouchDeltaMag - touchDeltaMag) *-1;
    33.  
    34.             transform.Translate(0, 0, deltaMagnitudeDiff);
    35.         }
    36.     }
    37. }
    PS: Anyone have an idea on how I can add a camera rotation?
    You know when you move two fingers clockwise or anticlockwise.
    Thanks again
    Aliquidhacker
     
    Last edited: Jun 13, 2015
    KB_sky likes this.
  13. AliquidHacker

    AliquidHacker

    Joined:
    Jan 10, 2015
    Posts:
    25
    @Chris Trueman any idea if there is a Input.mousePosition equivalent for touch input?
    Thanks
    AliquidHacker
     
  14. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    touch.position is its equivalent. Its a Vector2 where as Input.mousePosition is a Vector3 thus the reason I converted it in the code I posted.
     
  15. AliquidHacker

    AliquidHacker

    Joined:
    Jan 10, 2015
    Posts:
    25
    Ah, so no specific unity made line,
    I was not talking about your code, I was talking about in general,
    Thanks anyway.
    AliquidHacker
     
  16. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    I used Input.touches which is Unity specific to get a Touch that has the position, you can also use Input.GetTouch(0); to get it as well.

    I would suggest you review the video tutorial Unity has on MultiTouch Input.