Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Networking;
- using System;
- using Smooth;
- public class PlayerManager : NetworkBehaviour
- {
- private float speed = 4.0f;
- Vector3 velocity;
- Vector3 destination;
- Vector3 vectorBetween;
- Rigidbody m_rigidBody;
- bool reached = true;
- // Start is called before the first frame update
- void Start()
- {
- m_rigidBody = GetComponent<Rigidbody>();
- }
- // Update is called once per frame
- void Update()
- {
- if (!isClient) return;
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit hit;
- if (Input.GetButtonDown("Fire2"))
- {
- if (Physics.Raycast(ray, out hit, 100))
- {
- reached = false;
- destination = new Vector3(hit.point.x, 0.5f, hit.point.z);
- vectorBetween = destination - transform.position;
- CmdRotate(vectorBetween);
- }
- }
- if (reached == false)
- {
- vectorBetween = destination - transform.position;
- vectorBetween.Normalize();
- velocity = (vectorBetween) * 200.0f * Time.deltaTime;
- if (m_rigidBody.velocity.magnitude < 9.5f)
- {
- CmdMove(velocity * speed);
- }
- }
- if (Vector3.Distance(transform.position, destination) < 0.75f)
- {
- reached = true;
- }
- }
- [Command]
- void CmdMove(Vector3 v)
- {
- m_rigidBody.AddForce(v);
- Debug.Log("Move");
- }
- [Command]
- void CmdRotate(Vector3 v)
- {
- transform.rotation = Quaternion.LookRotation(new Vector3(v.x, 0f, v.z));
- Debug.Log("Rotate");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement