Search Unity

How to Move Camera Over Terrain Using Touch Input in Unity 3D

Discussion in 'iOS and tvOS' started by CreepyGnome, Jul 16, 2012.

  1. CreepyGnome

    CreepyGnome

    Joined:
    Oct 10, 2009
    Posts:
    9
    I am new to Unity and I am trying to figure out how to move the camera over a map/terrain using touch input. The camera would be looking down at the terrain with a rotation of (90,0,0). The terrain is on layer 8. I have had no problem getting it moving with keyboard, now I am trying to move to touch and it is very different if you want to keep expected usage on iOS.

    The best example I can think of on a built in iOS app is Maps where the user would touch the screen and that point on the map would stay under the finger as long as the finger stayed on the screen. So as the user moves their finger the map appears to be moving with the finger. I have not been able to find examples that show how to do it this way. I have seen may examples of moving the camera or character with the mouse but they don't seem to translate well to this style.

    Posted this in Unity Answers a few days ago and a few minutes ago on StackOverflow, feel free to answer their also.

    http://answers.unity3d.com/questions/283159/move-camera-over-terrain-using-touch-input.html

    http://stackoverflow.com/questions/11497085/move-camera-over-terrain-using-touch-input-in-unity-3d
     
  2. order66

    order66

    Joined:
    Apr 17, 2012
    Posts:
    150
    Have a look at the Standards Assets for Mobile which comes with Unity. You can import these when creating a new project or under Asset Store/My Stuff.

    There is an example called "Tap to Move". Have a look at this, it gives you a good starting point for your problem.
     
  3. CreepyGnome

    CreepyGnome

    Joined:
    Oct 10, 2009
    Posts:
    9
  4. pavlito

    pavlito

    Joined:
    Nov 27, 2007
    Posts:
    136
    You might want to try this. Cast a ray from screen point (touch) to world. Get the hit and lerp the camera accordingly.

    Code (csharp):
    1.  
    2. private RaycastHit hit;
    3. private Ray ray;
    4. private Vector3 mousePoint;
    5.  
    6. void Update()
    7. {
    8.       // Input.mousePosition works on a device without changing code
    9.       ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    10.       // Cast ray
    11.       Physics.Raycast(ray, out hit);
    12.       // Get ray's hit point
    13.       mousePoint = hit.point;
    14.       // Lerp
    15.       Camera.main.position = Vector3.Lerp(Camera.main.position, mousePoint, Time.deltaTime);
    16. }
    17.  
    You may need to control the Y axis of the camera so it goes up/down as it encounters slopes in your terrain.

    Code (csharp):
    1.  
    2. ...
    3. Camera.main.position = Vector3.Lerp(Camera.main.position, new Vector3(mousePoint.x, mousePoint.y   + cameraHeightOffset, mousePoint.z), Time.deltaTime);
    4.  
    I haven't tested this, but it should lerp the camera from its current position to your finger position. Since this is in Update() function, moving your finger changes the raycastHit point and lerps again.

    Good luck!
     
    Last edited: Jul 20, 2012