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 :: c# object list contains object returns incorrect boolean 
Csharp :: How to scroll to bottom of ListBox 
Csharp :: wpf user parent controller datacontext 
Csharp :: c# string is all zeros 
Csharp :: player not following slide object unity 2d 
Csharp :: C# Custom setter with parameter 
Csharp :: c# exec command output 
Csharp :: .net core api routing not working 
Csharp :: c# ipaddress to integer 
Csharp :: mock return exception c# 
Csharp :: c# directory entry invoke 
Csharp :: Implementing Banner Ads Unity 
Csharp :: c# string .contains against empty string returns 
Csharp :: how to download things c# 
Csharp :: compare 0001/01/01 in c# 
Csharp :: bootstrap daterangepicker change language to french 
Csharp :: asp.net framework mvc cors error axios 
Csharp :: unity organize variables in inspector 
Csharp :: ef core totable 
Csharp :: c# switch two values 
Csharp :: c# sort word 
Csharp :: imagetarget found event vuforia c# 
Csharp :: Align String with Spaces [C#] 
Csharp :: how to colapse all methods visual studio 
Csharp :: hacking 
Csharp :: grass download for unityh 
Csharp :: Razor do while loop 
Csharp :: unity Texture2D efficient performance draw pixels 
Csharp :: how to use external resource.resx file in c# 
Csharp :: unity screentoworldpoint 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =