Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5. using System;
  6. using Smooth;
  7.  
  8. public class PlayerManager : NetworkBehaviour
  9. {
  10.  
  11. private float speed = 4.0f;
  12.  
  13. Vector3 velocity;
  14. Vector3 destination;
  15. Vector3 vectorBetween;
  16. Rigidbody m_rigidBody;
  17.  
  18. bool reached = true;
  19.  
  20.  
  21. // Start is called before the first frame update
  22. void Start()
  23. {
  24. m_rigidBody = GetComponent<Rigidbody>();
  25. }
  26.  
  27. // Update is called once per frame
  28. void Update()
  29. {
  30.  
  31.  
  32. if (!isClient) return;
  33.  
  34. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  35. RaycastHit hit;
  36. if (Input.GetButtonDown("Fire2"))
  37. {
  38. if (Physics.Raycast(ray, out hit, 100))
  39. {
  40. reached = false;
  41. destination = new Vector3(hit.point.x, 0.5f, hit.point.z);
  42. vectorBetween = destination - transform.position;
  43. CmdRotate(vectorBetween);
  44. }
  45. }
  46.  
  47.  
  48.  
  49. if (reached == false)
  50. {
  51. vectorBetween = destination - transform.position;
  52. vectorBetween.Normalize();
  53. velocity = (vectorBetween) * 200.0f * Time.deltaTime;
  54.  
  55. if (m_rigidBody.velocity.magnitude < 9.5f)
  56. {
  57. CmdMove(velocity * speed);
  58. }
  59.  
  60. }
  61.  
  62. if (Vector3.Distance(transform.position, destination) < 0.75f)
  63. {
  64. reached = true;
  65. }
  66.  
  67.  
  68. }
  69.  
  70.  
  71.  
  72. [Command]
  73. void CmdMove(Vector3 v)
  74. {
  75. m_rigidBody.AddForce(v);
  76. Debug.Log("Move");
  77.  
  78. }
  79.  
  80. [Command]
  81. void CmdRotate(Vector3 v)
  82. {
  83. transform.rotation = Quaternion.LookRotation(new Vector3(v.x, 0f, v.z));
  84. Debug.Log("Rotate");
  85.  
  86. }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement