Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Player : MonoBehaviour
  6. {
  7. Vector3 playerPosition;
  8. Vector3 mouseF;
  9. Vector3 mouseN;
  10.  
  11. void FixedUpdate()
  12. {
  13. if (Input.GetMouseButton(0))
  14. {
  15. setMouseF();
  16. setMouseN();
  17.  
  18. Debug.DrawRay(mouseN, mouseF - mouseN, Color.green);
  19. Debug.DrawRay(mouseN, (playerPosition - mouseN), Color.red);
  20.  
  21. // move the player base on mouse position
  22. transform.position = mouseF;
  23. }
  24.  
  25. if (Input.GetMouseButtonUp(0))
  26. {
  27.  
  28. // on mouse release, set the new player position
  29. playerPosition = mouseF;
  30. }
  31.  
  32. }
  33.  
  34. void setMouseF()
  35. {
  36. Vector3 mouseFar = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10f);
  37. mouseF = Camera.main.ScreenToWorldPoint(mouseFar);
  38. }
  39.  
  40. void setMouseN()
  41. {
  42. Vector3 mouseNear = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane);
  43. mouseN = Camera.main.ScreenToWorldPoint(mouseNear);
  44. }
  45. }
  46.  
  47. Vector3 initialMousePos;
  48. Vector3 offset;
  49.  
  50. private void Update() {
  51. if(Input.GetMouseButtonDown(0))
  52. {
  53. initialMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  54. }
  55. if(Input.GetMouseButton(0))
  56. {
  57. offset = initialMousePos - Camera.main.ScreenToWorldPoint(Input.mousePosition);
  58. transform.position += offset;
  59. }
  60. }
  61.  
  62. using System.Collections;
  63. using System.Collections.Generic;
  64. using UnityEngine;
  65.  
  66. public class Player : MonoBehaviour
  67. {
  68. Vector3 playerPosition;
  69. Vector3 mouseF;
  70. Vector3 mouseN;
  71. Vector3 diff;
  72.  
  73. void FixedUpdate()
  74. {
  75. if (Input.GetMouseButtonDown(0))
  76. {
  77. InitPlayer();
  78. }
  79.  
  80. if (Input.GetMouseButton(0))
  81. {
  82. SetMouseF();
  83. SetMouseN();
  84.  
  85. Debug.DrawRay(mouseN, mouseF - mouseN, Color.green);
  86. Debug.DrawRay(mouseN, (mouseF + diff) - mouseN, Color.red);
  87.  
  88. // move the player base on mouse position
  89. transform.position = mouseF + diff;
  90. }
  91.  
  92. if (Input.GetMouseButtonUp(0))
  93. {
  94. }
  95.  
  96. }
  97.  
  98. void InitPlayer()
  99. {
  100. SetMouseF();
  101.  
  102. // set player position where the object is
  103. playerPosition = transform.position;
  104.  
  105. // get the distance between the player and mouse click
  106. diff = playerPosition - mouseF;
  107. }
  108.  
  109. void SetMouseF()
  110. {
  111. Vector3 mouseFar = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10f);
  112. mouseF = Camera.main.ScreenToWorldPoint(mouseFar);
  113. }
  114.  
  115. void SetMouseN()
  116. {
  117. Vector3 mouseNear = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane);
  118. mouseN = Camera.main.ScreenToWorldPoint(mouseNear);
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement