Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Leap.Unity.Swizzle;
  4. using UnityEngine;
  5. using UnityEngine.Networking;
  6. using Valve.VR;
  7.  
  8. public class RaftInit : MonoBehaviour
  9. {
  10.  
  11. public GameObject ParentedStuff;
  12. public GameObject SyncPosition;
  13. public GameObject ViveCam;
  14.  
  15. public NetworkController network;
  16.  
  17. [SyncVar]
  18. private Vector3 syncScale = new Vector3(1,1,1);
  19. [SyncVar]
  20. private bool isTurned = false;
  21.  
  22.  
  23.  
  24. [HideInInspector]
  25. public static Vector3 raftPosition;
  26.  
  27. // Start is called before the first frame update
  28. void Start()
  29. {
  30. Application.targetFrameRate = 100;
  31.  
  32.  
  33. if (network.host)
  34. {
  35.  
  36. var mesh = new Mesh();
  37.  
  38. var rect = new Valve.VR.HmdQuad_t();
  39. SteamVR_PlayArea.GetBounds(SteamVR_PlayArea.Size.Calibrated, ref rect);
  40.  
  41. Vector3 newScale = checkAndCorrectRotation(rect);
  42.  
  43. mesh.vertices = gameObject.GetComponent<MeshFilter>().mesh.vertices;
  44. mesh.triangles = gameObject.GetComponent<MeshFilter>().mesh.triangles;
  45.  
  46. if (newScale.x != 0 && newScale.z != 0)
  47. {
  48. transform.localScale = newScale;
  49. syncScale = newScale;
  50. }
  51.  
  52. var vertices = mesh.vertices;
  53. var offset = new Vector3(-0.5f, -0.2f, -0.5f);
  54. Matrix4x4 scale = Matrix4x4.Scale(transform.localScale);
  55. scale = Matrix4x4.Scale(new Vector3(0.5f, 1, 0.5f)) * scale;
  56. var mat = transform.localToWorldMatrix;
  57. for (int i = 0; i < vertices.Length; i++)
  58. {
  59. vertices[i] = scale.MultiplyPoint(vertices[i]);
  60. }
  61.  
  62. print(vertices);
  63. mesh.vertices = vertices;
  64. mesh.RecalculateBounds();
  65. mesh.RecalculateNormals();
  66. mesh.RecalculateTangents();
  67.  
  68.  
  69. GetComponent<FloatingGameEntityRealist>().buoyancyMesh = mesh;
  70. GetComponent<FloatingGameEntityRealist>().updateMesh();
  71. ParentedStuff.transform.parent = transform;
  72. }
  73.  
  74. }
  75.  
  76. public void SyncRaft()
  77. {
  78. if (!network.host)
  79. {
  80. transform.localScale = syncScale;
  81.  
  82. if (isTurned)
  83. {
  84. ParentedStuff.transform.Rotate(0, 90, 0);
  85.  
  86. }
  87.  
  88. ParentedStuff.transform.position = transform.position;
  89. ParentedStuff.transform.rotation = transform.rotation;
  90. ParentedStuff.transform.parent = transform;
  91. }
  92. }
  93.  
  94. private Vector3 checkAndCorrectRotation(HmdQuad_t rect)
  95. {
  96. print(rect.vCorners0.v0 + " - " + rect.vCorners0.v2);
  97.  
  98. if (Mathf.Abs(rect.vCorners0.v0) >= Mathf.Abs(rect.vCorners0.v2))
  99. {
  100. print("x bigger");
  101.  
  102. //transform.forward = Quaternion.Euler(0, 90, 0) * transform.forward;
  103. ParentedStuff.transform.Rotate(0, 90, 0);
  104. isTurned = true;
  105. return new Vector3(Mathf.Abs(rect.vCorners0.v2 - rect.vCorners2.v2), this.transform.localScale.y, Mathf.Abs(rect.vCorners0.v0 - rect.vCorners2.v0));
  106.  
  107. }
  108. else
  109. {
  110. print("y bigger");
  111. return new Vector3(Mathf.Abs(rect.vCorners0.v0 - rect.vCorners2.v0), this.transform.localScale.y, Mathf.Abs(rect.vCorners0.v2 - rect.vCorners2.v2));
  112.  
  113. }
  114.  
  115. }
  116.  
  117. // Update is called once per frame
  118. void Update()
  119. {
  120. raftPosition = transform.position;
  121.  
  122. var velocity = transform.InverseTransformDirection(GetComponent<Rigidbody>().velocity).z;
  123. //print(velocity);
  124.  
  125. if (Input.GetKey(KeyCode.W))
  126. {
  127. GetComponent<Rigidbody>().AddForce(transform.forward * 1000);
  128. print("MOOOVE");
  129.  
  130. }
  131. if (Input.GetKey(KeyCode.A))
  132. {
  133. GetComponent<Rigidbody>().AddForceAtPosition(transform.right * 100 * velocity, transform.localPosition + -transform.forward * 5);
  134. }
  135. if (Input.GetKey(KeyCode.D))
  136. {
  137. GetComponent<Rigidbody>().AddForceAtPosition(-transform.right * 100 * velocity, transform.localPosition + -transform.forward * 5);
  138.  
  139. }
  140.  
  141.  
  142.  
  143. SyncPosition.transform.position = transform.position;
  144. //ViveCam.transform.up = Vector3.up;
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement