Created
November 17, 2017 01:03
-
-
Save theta360developers/ae7e7d86fbe9a9739d39ebe81ea57deb to your computer and use it in GitHub Desktop.
Follow gaze in Unity for THETA 360 images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class followView : MonoBehaviour { | |
bool mouseDown = false; | |
float mouseX; | |
float mouseY; | |
Camera mainCamera; | |
void Start () { | |
mainCamera = GetComponent<Camera>(); | |
} | |
void Update () { | |
if(Input.GetMouseButtonDown(0) && !mouseDown ) | |
{ | |
mouseDown = true; | |
mouseX = Input.mousePosition.x; | |
mouseY = Input.mousePosition.y; | |
} | |
else if(Input.GetMouseButtonUp(0) && mouseDown) | |
{ | |
mouseDown = false; | |
} | |
} | |
void LateUpdate() | |
{ | |
if(mouseDown) | |
{ | |
float mouseXStop = Input.mousePosition.x; | |
float mouseYStop = Input.mousePosition.y; | |
float deltaX = mouseXStop - mouseX; | |
float deltaY = mouseYStop - mouseY; | |
float centerXNew = Screen.width / 2 + deltaX; | |
float centerYNew = Screen.height / 2 + deltaY; | |
Vector3 Gaze = mainCamera.ScreenToWorldPoint(new Vector3(centerXNew, centerYNew, mainCamera.nearClipPlane)); | |
transform.LookAt(Gaze); | |
mouseX = mouseXStop; | |
mouseY = mouseYStop; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment