Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

C# Move Camera Over Terrain Using Touch Input In Unity 3D

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

C# Move Camera Over Terrain Using Touch Input In Unity 3D - Append To Camera

private Vector2 worldStartPoint;

void Update () {

    // only work with one touch
    if (Input.touchCount == 1) {
        Touch currentTouch = Input.GetTouch(0);

        if (currentTouch.phase == TouchPhase.Began) {
            this.worldStartPoint = this.getWorldPoint(currentTouch.position);
        }

        if (currentTouch.phase == TouchPhase.Moved) {
            Vector2 worldDelta = this.getWorldPoint(currentTouch.position) - this.worldStartPoint;

            Camera.main.transform.Translate(
                -worldDelta.x,
                -worldDelta.y,
                0
            );
        }
    }
}

// convert screen point to world point
private Vector2 getWorldPoint (Vector2 screenPoint) {
    RaycastHit hit;
    Physics.Raycast(Camera.main.ScreenPointToRay(screenPoint), out hit);
    return hit.point;
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: How to execute a script after the c# function executed 
Csharp :: print all string in textbox in array c# 
Csharp :: c# language 
Csharp :: last word of string to uppercase c# 
Csharp :: convert iqueryable to list c# 
Csharp :: C# resize window without title bar 
Csharp :: unity mix gradient colors 
Csharp :: save current dir shell 
Csharp :: Difference between IHostingEnvironment and IWebHostEnvironment ? 
Csharp :: ignore collision unity 2d 
Csharp :: lambda distinct by property 
Csharp :: c# if loop 
Csharp :: ado .net nullable int datareader 
Csharp :: CS0234 compile error c# unity fix scene managment 
Csharp :: set teh screen rect of camera unity 
Csharp :: how to do multiplication with button c# 
Csharp :: c# how to load type of class from string 
Csharp :: Last N lines from file 
Csharp :: C# system dont let write txt file 
Csharp :: winforms lifecycle 
Csharp :: c# wpf datagrid extra column 
Csharp :: anidate bucle in c# 
Csharp :: unity customize hierarchy window 
Csharp :: unity C# add torque to rigidbody 
Csharp :: how to execute a function in every object of list c# 
Csharp :: get centerpoint of points transforms 
Csharp :: using == is inefficient unity 
Csharp :: viewsheet location revit api 
Csharp :: Connect To MongoDB From A Different Machine 
Csharp :: c# instantiation 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =