Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

moving camera with touch screen unity

using UnityEngine;
using System.Collections;

public class ViewDrag : MonoBehaviour {
Vector3 hit_position = Vector3.zero;
Vector3 current_position = Vector3.zero;
Vector3 camera_position = Vector3.zero;
float z = 0.0f;

// Use this for initialization
void Start () {

}

void Update(){
    if(Input.GetMouseButtonDown(0)){
        hit_position = Input.mousePosition;
        camera_position = transform.position;

    }
    if(Input.GetMouseButton(0)){
        current_position = Input.mousePosition;
        LeftMouseDrag();        
    }
}

void LeftMouseDrag(){
    // From the Unity3D docs: "The z position is in world units from the camera."  In my case I'm using the y-axis as height
    // with my camera facing back down the y-axis.  You can ignore this when the camera is orthograhic.
    current_position.z = hit_position.z = camera_position.y;

    // Get direction of movement.  (Note: Don't normalize, the magnitude of change is going to be Vector3.Distance(current_position-hit_position)
    // anyways.  
    Vector3 direction = Camera.main.ScreenToWorldPoint(current_position) - Camera.main.ScreenToWorldPoint(hit_position);

    // Invert direction to that terrain appears to move with the mouse.
    direction = direction * -1;

    Vector3 position = camera_position + direction;

    transform.position = position;
}
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: update browserslist 
Csharp :: monegame deltatime 
Csharp :: What are logic gates? 
Csharp :: allow scroll with wheel mouse datagridview c# 
Csharp :: Screen.lockcursor unity 
Csharp :: async where linq 
Csharp :: unity inspector draw line 
Csharp :: translate int to string with x 0 before c# 
Csharp :: ontriggerenter2d 
Csharp :: c# external ip 
Csharp :: c# Case insensitive Contains(string) 
Csharp :: c# object is enum 
Csharp :: join string c# 
Csharp :: Send Hotmail, Outlook, Office365 Email using SMTP C# .NET 
Csharp :: dictionaries in unity 
Csharp :: c# on variable change property get set 
Csharp :: load a form from button c# 
Csharp :: c# mock ref parameter 
Csharp :: how to iterate a generic list in c# 
Csharp :: c# system cryptography hash string 
Csharp :: linq contains 
Csharp :: .net core get runtime version 
Csharp :: sends keys enter selenium c# 
Csharp :: unity interface 
Csharp :: how to return array in function c# 
Csharp :: serialize object to json 
Csharp :: convert xml to json 
Csharp :: remove numericUpDown arrows 
Csharp :: c# async task constructor 
Csharp :: rotate skybox on x axis unity 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =