Created
April 6, 2020 02:31
-
-
Save knightcube/a48f93cc8fbf4866bbecb8e057a8c58c to your computer and use it in GitHub Desktop.
This file contains hidden or 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 System.Text; | |
using UnityEngine; | |
using UnityEngine.Networking; | |
public class SnapMovement : MonoBehaviour | |
{ | |
public GameObject markerBtn; | |
private bool isLocked = false; | |
private MyData myData; | |
private TriggerTest triggerTest; | |
private Renderer lockBtnRenderer; | |
Vector3 destination; | |
private bool isMoving = false; | |
void Start() | |
{ | |
triggerTest = markerBtn.GetComponent<TriggerTest>(); | |
lockBtnRenderer = triggerTest.lockBtn.GetComponent<Renderer>(); | |
destination = new Vector3(transform.position.x, transform.position.y, transform.position.z); | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
OVRInput.Update(); | |
if ((Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.W) || OVRInput.GetDown(OVRInput.Button.PrimaryTouchpad)) && !isMoving) | |
{ | |
//transform.Translate(Vector3.forward); | |
destination = new Vector3(transform.position.x, transform.position.y, transform.position.z + 1); | |
lockBtnRenderer.sharedMaterial.color = Color.white; | |
isLocked = false; | |
isMoving = true; | |
} | |
if ((Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.S) || OVRInput.GetDown(OVRInput.Touch.PrimaryTouchpad)) && !isMoving) | |
{ | |
//transform.Translate(Vector3.back); | |
destination = new Vector3(transform.position.x, transform.position.y, transform.position.z - 1); | |
lockBtnRenderer.sharedMaterial.color = Color.white; | |
isLocked = false; | |
isMoving = true; | |
} | |
if ((Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A)) && !isMoving) | |
{ | |
transform.localEulerAngles = new Vector3(0, -90, 0); | |
} | |
if ((Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D)) && !isMoving) | |
{ | |
transform.localEulerAngles = new Vector3(0, 0, 0); | |
} | |
transform.position = Vector3.MoveTowards(transform.position, destination, 1f * Time.deltaTime); | |
if(transform.position == destination) | |
{ | |
// if object has reached destination it should not move anymore | |
isMoving = false; | |
} | |
if (Input.GetKeyDown(KeyCode.Space) && isOnMarker() && !isMoving) | |
{ | |
RaycastHit hit; | |
if (isLocked) | |
{ | |
lockBtnRenderer.sharedMaterial.color = Color.white; | |
isLocked = false; | |
} | |
else | |
{ | |
// Button locked | |
lockBtnRenderer.sharedMaterial.color = Color.green; | |
isLocked = true; | |
// Send data | |
SendData(); | |
} | |
} | |
if(GetButtonKeyOculus() && isOnMarker() && !isMoving) | |
{ | |
Renderer lockBtnRenderer = triggerTest.lockBtn.GetComponent<Renderer>(); | |
if (isLocked) | |
{ | |
lockBtnRenderer.sharedMaterial.color = Color.white; | |
isLocked = false; | |
} | |
else | |
{ | |
// Button locked | |
lockBtnRenderer.sharedMaterial.color = Color.green; | |
isLocked = true; | |
// Send data | |
SendData(); | |
} | |
} | |
} | |
private bool GetButtonKeyOculus() | |
{ | |
// returns true if right-handed controller connected | |
if (!OVRInput.IsControllerConnected(OVRInput.Controller.RTrackedRemote)) | |
{ | |
//Debug.Log("Oculus Right Hand Controller is not connected"); | |
return false; | |
} | |
return OVRInput.GetDown(OVRInput.Button.PrimaryIndexTrigger) || OVRInput.Get(OVRInput.Button.PrimaryIndexTrigger) || OVRInput.GetUp(OVRInput.Button.PrimaryIndexTrigger); | |
} | |
private bool isOnMarker() | |
{ | |
if (transform.position.x == 0) return true; | |
else { | |
//Debug.Log("player is not on marker or the path"); | |
return false; | |
} | |
} | |
private void SendData() | |
{ | |
string coordinates = transform.position.x + "," + transform.position.y + "," + transform.position.z; | |
string positionNumber = "" + (20 - Math.Abs(transform.position.z)); | |
myData = new MyData(); | |
myData.coordinates = coordinates; | |
myData.position = positionNumber; | |
string jsonResult = JsonUtility.ToJson(myData); | |
StartCoroutine(Upload(jsonResult)); | |
} | |
IEnumerator Upload(string jsonResult) | |
{ | |
string url = "knightcube.pythonanywhere.com"; | |
Debug.Log("JSON -" + jsonResult); | |
WWWForm form = new WWWForm(); | |
form.AddField("position", myData.position); | |
form.AddField("coordinates", myData.coordinates); | |
Debug.Log("Form -" + form.ToString()); | |
using (UnityWebRequest www = UnityWebRequest.Post(url, form)) | |
{ | |
yield return www.SendWebRequest(); | |
if (www.isNetworkError || www.isHttpError) | |
{ | |
Debug.Log(www.error); | |
} | |
else | |
{ | |
Debug.Log("Form upload complete!"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment