Th3NiKo

Kursor3D

May 21st, 2018
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. /*
  6.  
  7. Na przyszlosc zrobic z tego singleton
  8.  
  9. */
  10. public class Kursor3D : MonoBehaviour {
  11.  
  12. Vector3 position;
  13.  
  14. public Material Clicked;
  15. public Material NonClicked;
  16.  
  17. bool isClicked;
  18. bool isPressed;
  19. Collider obj;
  20. Vector3 lastPosition;
  21. public Vector3 delta;
  22.  
  23. float t;
  24. void Start () {
  25. t = 0.1f;
  26. position = new Vector3(0.0f, 0.0f, 0.0f);
  27. isClicked = false;
  28. lastPosition = position;
  29. obj = null;
  30. isPressed = false;
  31. }
  32.  
  33.  
  34. void Update () {
  35. if(Input.GetKey(KeyCode.LeftArrow)){
  36. position = Vector3.Lerp(position, position + new Vector3(-0.1f, 0.0f, 0.0f),t);
  37. } if(Input.GetKey(KeyCode.RightArrow)){
  38. position = Vector3.Lerp(position,position + new Vector3(0.1f, 0.0f, 0.0f),t);
  39. }
  40. if(Input.GetKey(KeyCode.DownArrow)){
  41. position = Vector3.Lerp(position, position + new Vector3(0.0f, 0.0f, -0.1f),t);
  42. } if(Input.GetKey(KeyCode.UpArrow)){
  43. position = Vector3.Lerp(position, position + new Vector3(0.0f, 0.0f, 0.1f),t);
  44. }
  45.  
  46. if(Input.GetKey(KeyCode.Q)){
  47. position = Vector3.Lerp(position, position + new Vector3(0.0f, 0.1f, 0.0f),t);
  48. } if(Input.GetKey(KeyCode.E)){
  49. position = Vector3.Lerp(position, position + new Vector3(0.0f, -0.1f, 0.0f),t);
  50. }
  51.  
  52. if(Input.GetKey(KeyCode.LeftShift)){
  53. isClicked = !isClicked;
  54. }
  55.  
  56. if(Input.GetKey(KeyCode.LeftShift)){
  57. isPressed = true;
  58. } else {
  59. isPressed = false;
  60. }
  61.  
  62. if(obj != null){
  63. if(isClicked){
  64. obj.transform.position = Vector3.Lerp(obj.transform.position, (position - lastPosition) + obj.transform.position,1.0f);
  65. //obj.transform.SetParent(this.transform, true);
  66. }
  67.  
  68. }
  69. delta = lastPosition - position;
  70. lastPosition = position;
  71.  
  72. }
  73.  
  74. public Vector3 GetPosition(){
  75. return position;
  76. }
  77.  
  78. public bool IsPressed(){
  79. return isPressed;
  80. }
  81.  
  82.  
  83. void OnTriggerEnter(Collider other) {
  84.  
  85. if(other.tag == "Block"){
  86. obj = other;
  87. if(isPressed){
  88. other.GetComponent<Rigidbody>().useGravity = false;
  89. other.GetComponent<Rigidbody>().isKinematic = true;
  90. } else {
  91. other.GetComponent<Rigidbody>().useGravity = true;
  92. other.GetComponent<Rigidbody>().isKinematic = false;
  93. }
  94. other.GetComponent<MeshRenderer>().material = Clicked;
  95. }
  96. }
  97.  
  98. void OnTriggerStay(Collider other)
  99. {
  100. if(other.tag == "Block"){
  101. other.GetComponent<MeshRenderer>().material = Clicked;
  102.  
  103. if(isClicked){
  104. obj = other;
  105. other.GetComponent<Rigidbody>().useGravity = false;
  106. other.GetComponent<Rigidbody>().isKinematic = true;
  107. } else {
  108. other.GetComponent<Rigidbody>().useGravity = true;
  109. other.GetComponent<Rigidbody>().isKinematic = false;
  110. }
  111. }
  112. }
  113.  
  114. void OnTriggerExit(Collider other) {
  115.  
  116. if(other.tag == "Block"){
  117. obj = null;
  118. if(!isClicked){
  119. other.GetComponent<Rigidbody>().useGravity = true;
  120. other.GetComponent<Rigidbody>().isKinematic = false;
  121. other.GetComponent<MeshRenderer>().material = NonClicked;
  122. }
  123.  
  124. }
  125. }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment