Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using UMA;
- public class PlayerHandler : MonoBehaviour
- {
- private Client myClient = null;
- public NetworkPlayer TargetPlayerTradePlayer;
- public PlayerHandler TargetPlayerTradeHandler;
- public bool AcceptedTrade = false;
- public bool CancelledTrade = false;
- public ItemData[] TradeItems = new ItemData[5];
- public int[] TradeItemSlots = new int[5];
- public int currentslot = 0;
- public string[] slottypes = new string[5];
- public string slottype = "";
- bool firstinput = false;
- public bool Initialized = false;
- public string OWNERPLAYER;
- public string OWNERPLAYERGUID;
- public string OWNERUSER;
- public string VIEWID;
- public int TYPE = 0;
- private static GameObject myPlayerCameraGO;
- public GameObject myPlayerGO;
- private GameObject CameraLookAt;
- public NetworkPlayer myPlayer;
- private NetworkViewID myPlayerViewID;
- private NetworkViewID myPlayerCameraViewID;
- private Rigidbody myRigidbody;
- private static Camera myPlayerCamera;
- private Animator myPlayerAnimator;
- public int currenticon = 0;
- public ItemData currentitem;
- public float MouseX = 0f;
- public float MouseY = 0f;
- public float Distance = 5f;
- public float desiredDistance = 0f;
- public float distanceSmooth = 0f;
- public float preOccludedDistance = 0f;
- public float velX = 0f;
- public float velY = 0f;
- public float velZ = 0f;
- public float velDistance = 0f;
- public Vector3 MoveVector = Vector3.zero;
- public Vector3 position = Vector3.zero;
- public Vector3 desiredPosition = Vector3.zero;
- public float ForwardSpeed = 3f;
- public float BackwardSpeed = 1f;
- public float StrafingSpeed = 2f;
- public float JumpSpeed = 5f;
- public float TerminalVelocity = 5f;
- public float Gravity = -15f;
- int Grounded = 0;
- bool MouseButton1 = false;
- bool MouseButton0 = false;
- bool MouseButton2 = false;
- bool MouseButton1Up = false;
- bool MouseButton0Up = false;
- bool MouseButton2Up = false;
- bool MouseButton1Down = false;
- bool MouseButton0Down = false;
- bool MouseButton2Down = false;
- bool TabDown = false;
- bool Jumping = false;
- float HAxis;
- float VAxis;
- Vector3 MousePosition;
- float MouseXAxis;
- float MouseYAxis;
- float MouseScrollAxis = -1f;
- private bool PlayerIsCombative = false;
- public void Initialize(int type, NetworkPlayer player, string user, NetworkViewID viewID, NetworkViewID cameraViewID, GameObject camLookAt, Animator animator, Camera camera, GameObject camerago, GameObject playergo, Rigidbody rigidbody)
- {
- if(Network.player == player || Network.isServer)
- enabled=true;
- else
- enabled=false;
- myPlayer = player;
- CameraLookAt = camLookAt;
- myPlayerViewID = viewID;
- myPlayerAnimator = animator;
- OWNERPLAYER = myPlayer.ToString();
- OWNERPLAYERGUID = myPlayer.guid;
- OWNERUSER = user;
- VIEWID = myPlayerViewID.ToString();
- TYPE = type;
- myPlayerCameraGO = camerago;
- myPlayerCamera = camera;
- myPlayerCameraViewID = cameraViewID;
- myRigidbody = rigidbody;
- myPlayerGO = playergo;
- Initialized = true;
- TradeItems = new ItemData[5];
- TradeItemSlots = new int[5];
- for(int i = 0;i<5;i++)
- {
- TradeItems[i] = ItemData.EmptyItem();
- TradeItemSlots[i]= 0;
- slottypes[i] = "";
- }
- }
- private void FixedUpdate()
- {
- if(Network.isServer)
- {
- Vector3 gravityUp;
- gravityUp = myPlayerGO.transform.position - new Vector3(0,0,0);
- gravityUp.Normalize();
- myRigidbody.AddForce(gravityUp * Gravity * myRigidbody.mass);
- if(Grounded >= 1)
- myRigidbody.drag = 0.1f;
- else
- myRigidbody.drag = 1.0f;
- Quaternion quatern = Quaternion.FromToRotation(myPlayerGO.transform.up, gravityUp);
- quatern = quatern * myPlayerGO.transform.rotation;
- myPlayerGO.transform.rotation = Quaternion.Slerp(myPlayerGO.transform.rotation, quatern, 0.1f);
- DetermineDirection();
- if(myRigidbody.velocity.magnitude < MoveSpeed() && Grounded >= 1)
- {
- myRigidbody.AddForce(transform.rotation * Vector3.forward * VAxis * MoveSpeed());
- myRigidbody.AddForce(transform.rotation * Vector3.right * HAxis * MoveSpeed());
- }
- if(Jumping == true && Grounded >= 1)
- {
- myRigidbody.velocity = myRigidbody.velocity + (myRigidbody.transform.up * JumpSpeed);
- Jumping = false;
- }
- if (!PlayerIsCombative)
- {
- if(MouseButton2)
- {
- MouseX += MouseXAxis * 5f;
- MouseY -= MouseYAxis * 5f;
- if (MouseY < -360) MouseY += 360;
- if (MouseY > 360) MouseY -= 360;
- //MouseY = Mathf.Clamp(MouseY, -40f, 80f);
- }
- if(MouseScrollAxis < -0.01f || MouseScrollAxis > 0.01f)
- {
- desiredDistance = Mathf.Clamp(Distance - MouseScrollAxis * 5f,0.5f,5f);
- preOccludedDistance = desiredDistance;
- distanceSmooth = 0.05f;
- }
- int count = 0;
- do{
- if(desiredDistance < preOccludedDistance)
- {
- Vector3 pos = CalculatePosition(MouseY, MouseX, preOccludedDistance);
- float nearestDistance = CheckCameraPoints(CameraLookAt.transform.localPosition, pos);
- if(nearestDistance == -1 || nearestDistance > preOccludedDistance)
- {
- desiredDistance = preOccludedDistance;
- }
- }
- Distance = Mathf.SmoothDamp(Distance, desiredDistance, ref velDistance, distanceSmooth);
- desiredPosition = CalculatePosition(MouseY, MouseX, Distance);
- count++;
- }while(CheckIfOccluded(count));
- CheckCameraPoints(CameraLookAt.transform.localPosition, desiredPosition);
- float posX = Mathf.SmoothDamp(position.x, desiredPosition.x, ref velX, 0.05f);
- float posY = Mathf.SmoothDamp(position.y, desiredPosition.y, ref velY, 0.1f);
- float posZ = Mathf.SmoothDamp(position.z, desiredPosition.z, ref velZ, 0.05f);
- position = new Vector3(posX, posY, posZ);
- myPlayerCameraGO.transform.localPosition = position;
- Vector3 relPlayerPosition = CameraLookAt.transform.position - myPlayerCameraGO.transform.position;
- myPlayerCameraGO.transform.rotation = Quaternion.LookRotation(relPlayerPosition, myPlayerGO.transform.up);
- if (HAxis != 0 || VAxis != 0)
- {
- //myPlayerGO.transform.rotation = Quaternion.Euler(0,myPlayerCameraGO.transform.localEulerAngles.y,0);
- //myPlayerGO.transform.rotation = Quaternion.Euler(0,myPlayerCameraGO.transform.eulerAngles.y,0);
- //myPlayerGO.transform.localRotation = Quaternion.Euler(0,myPlayerCameraGO.transform.localEulerAngles.y,0);
- //myPlayerGO.transform.localRotation = Quaternion.Euler(0,myPlayerCameraGO.transform.eulerAngles.y,0);
- //myPlayerGO.transform.rotation = Quaternion.Euler(myPlayerGO.transform.eulerAngles.x,myPlayerCameraGO.transform.eulerAngles.y,myPlayerGO.transform.eulerAngles.z);
- //myPlayerGO.transform.rotation = Quaternion.Euler(myPlayerGO.transform.localEulerAngles.x,myPlayerCameraGO.transform.eulerAngles.y,myPlayerGO.transform.localEulerAngles.z);
- //myPlayerGO.transform.rotation = Quaternion.Euler(myPlayerGO.transform.eulerAngles.x,myPlayerCameraGO.transform.eulerAngles.y,myPlayerGO.transform.eulerAngles.z);
- //myPlayerGO.transform.rotation = Quaternion.Euler(myPlayerGO.transform.localEulerAngles.x,myPlayerCameraGO.transform.localEulerAngles.y,myPlayerGO.transform.localEulerAngles.z);
- //myPlayerGO.transform.localRotation = Quaternion.Euler(myPlayerGO.transform.eulerAngles.x,myPlayerCameraGO.transform.eulerAngles.y,myPlayerGO.transform.eulerAngles.z);
- //myPlayerGO.transform.localRotation = Quaternion.Euler(myPlayerGO.transform.localEulerAngles.x,myPlayerCameraGO.transform.eulerAngles.y,myPlayerGO.transform.localEulerAngles.z);
- //myPlayerGO.transform.localRotation = Quaternion.Euler(myPlayerGO.transform.eulerAngles.x,myPlayerCameraGO.transform.localEulerAngles.y,myPlayerGO.transform.eulerAngles.z);
- //myPlayerGO.transform.localRotation = Quaternion.Euler(myPlayerGO.transform.localEulerAngles.x,myPlayerCameraGO.transform.localEulerAngles.y,myPlayerGO.transform.localEulerAngles.z);
- }
- }
- }
- if(Network.isClient)
- {
- HAxis = Input.GetAxis("Horizontal");
- VAxis = Input.GetAxis("Vertical");
- MousePosition = Input.mousePosition;
- MouseXAxis = Input.GetAxis("Mouse X");
- MouseYAxis = Input.GetAxis("Mouse Y");
- MouseScrollAxis = Input.GetAxis("Mouse ScrollWheel");
- Jumping = Input.GetButton("Jump");
- if(Input.GetMouseButton(0))MouseButton0 = true;
- else MouseButton0 = false;
- if(Input.GetMouseButtonUp(0))MouseButton0Up = true;
- else MouseButton0Up = false;
- if(Input.GetMouseButtonDown(0))MouseButton0Down = true;
- else MouseButton0Down = false;
- if(Input.GetMouseButton(1))MouseButton1 = true;
- else MouseButton1 = false;
- if(Input.GetMouseButtonUp(1))MouseButton1Up = true;
- else MouseButton1Up = false;
- if(Input.GetMouseButtonDown(1))MouseButton1Down = true;
- else MouseButton1Down = false;
- if(Input.GetMouseButtonDown(2))MouseButton2Down = true;
- else MouseButton2Down = false;
- if(Input.GetMouseButtonUp(2))MouseButton2Up = true;
- else MouseButton2Up = false;
- if(Input.GetMouseButton(2))MouseButton2 = true;
- else MouseButton2 = false;
- if(Input.GetKeyDown(KeyCode.Tab))TabDown = true;
- else TabDown = false;
- }
- }
- private float MoveSpeed()
- {
- var moveSpeed = 0f;
- switch (MoveDirection)
- {
- case Direction.Stationary:
- moveSpeed = 0;
- break;
- case Direction.Forward:
- moveSpeed = ForwardSpeed;
- break;
- case Direction.Backward:
- moveSpeed = BackwardSpeed;
- break;
- case Direction.Left:
- moveSpeed = StrafingSpeed;
- break;
- case Direction.Right:
- moveSpeed = StrafingSpeed;
- break;
- case Direction.LeftForward:
- moveSpeed = ForwardSpeed;
- break;
- case Direction.RightForward:
- moveSpeed = ForwardSpeed;
- break;
- case Direction.LeftBackward:
- moveSpeed = BackwardSpeed;
- break;
- case Direction.RightBackward:
- moveSpeed = BackwardSpeed;
- break;
- }
- return moveSpeed;
- }
- private enum Direction
- {
- Stationary, Forward, Backward, Left, Right, LeftForward, RightForward, LeftBackward, RightBackward
- }
- private Direction MoveDirection {get; set;}
- private void DetermineDirection()
- {
- bool forward = false;
- bool backward = false;
- bool left = false;
- bool right = false;
- if(VAxis > 0)
- forward = true;
- if(VAxis < 0)
- backward = true;
- if(HAxis > 0)
- right = true;
- if(HAxis < 0)
- left = true;
- if(forward)
- {
- if(left)
- MoveDirection = Direction.LeftForward;
- else if(right)
- MoveDirection = Direction.RightForward;
- else
- MoveDirection = Direction.Forward;
- }
- else if(backward)
- {
- if(left)
- MoveDirection = Direction.LeftBackward;
- else if(right)
- MoveDirection = Direction.RightBackward;
- else
- MoveDirection = Direction.Backward;
- }
- else if(left)
- MoveDirection = Direction.Left;
- else if(right)
- MoveDirection = Direction.Right;
- else
- MoveDirection = Direction.Stationary;
- }
- private void CalculateDesiredPosition()
- {
- if(desiredDistance < preOccludedDistance)
- {
- Vector3 pos = CalculatePosition(MouseY, MouseX, preOccludedDistance);
- float nearestDistance = CheckCameraPoints(CameraLookAt.transform.localPosition, pos);
- if(nearestDistance == -1 || nearestDistance > preOccludedDistance)
- {
- desiredDistance = preOccludedDistance;
- }
- }
- Distance = Mathf.SmoothDamp(Distance, desiredDistance, ref velDistance, distanceSmooth);
- desiredPosition = CalculatePosition(MouseY, MouseX, Distance);
- }
- private Vector3 CalculatePosition(float rotationX, float rotationY, float distance)
- {
- Vector3 direction = new Vector3(0, 0, -distance);
- Quaternion rotation = Quaternion.Euler(rotationX, rotationY, 0);
- return CameraLookAt.transform.localPosition + rotation * direction;
- }
- private bool CheckIfOccluded(int count)
- {
- bool isOccluded = false;
- float nearestDistance = CheckCameraPoints(CameraLookAt.transform.localPosition, desiredPosition);
- if(nearestDistance != -1)
- {
- if(count < 10)
- {
- isOccluded = true;
- Distance -= 0.05f;
- if(Distance < 0.25f)
- Distance = 0.25f;
- }
- else
- Distance = nearestDistance - myPlayerCamera.nearClipPlane;
- desiredDistance = Distance;
- distanceSmooth = 1f;
- }
- return isOccluded;
- }
- private float CheckCameraPoints(Vector3 from, Vector3 to)
- {
- var nearDistance = -1f;
- RaycastHit hitInfo;
- ClipPlanePoints clipPlanePoints = ClipPlaneAtNear( to);
- if(Physics.Linecast(from, clipPlanePoints.UpperLeft, out hitInfo) && hitInfo.collider.tag != "Player")
- nearDistance = hitInfo.distance;
- if(Physics.Linecast(from, clipPlanePoints.LowerLeft, out hitInfo) && hitInfo.collider.tag != "Player")
- if(hitInfo.distance < nearDistance || nearDistance == -1)
- nearDistance = hitInfo.distance;
- if(Physics.Linecast(from, clipPlanePoints.UpperRight, out hitInfo) && hitInfo.collider.tag != "Player")
- if(hitInfo.distance < nearDistance || nearDistance == -1)
- nearDistance = hitInfo.distance;
- if(Physics.Linecast(from, clipPlanePoints.LowerRight, out hitInfo) && hitInfo.collider.tag != "Player")
- if(hitInfo.distance < nearDistance || nearDistance == -1)
- nearDistance = hitInfo.distance;
- if(Physics.Linecast(from, to + transform.forward * -myPlayerCamera.nearClipPlane, out hitInfo) && hitInfo.collider.tag != "Player")
- if(hitInfo.distance < nearDistance || nearDistance == -1)
- nearDistance = hitInfo.distance;
- return nearDistance;
- }
- private static ClipPlanePoints ClipPlaneAtNear(Vector3 pos)
- {
- ClipPlanePoints clipPlanePoints = new ClipPlanePoints();
- Transform transform = myPlayerCameraGO.transform;
- float HalfFOV = (myPlayerCamera.fieldOfView / 2) * Mathf.Deg2Rad;
- float aspect = myPlayerCamera.aspect;
- float distance = myPlayerCamera.nearClipPlane;
- float height = distance * Mathf.Tan(HalfFOV);
- float width = height * aspect;
- clipPlanePoints.LowerRight = pos + transform.right * width;
- clipPlanePoints.LowerRight -= transform.up * height;
- clipPlanePoints.LowerRight += transform.forward * distance;
- clipPlanePoints.LowerLeft = pos - transform.right * width;
- clipPlanePoints.LowerLeft -= transform.up * height;
- clipPlanePoints.LowerLeft += transform.forward * distance;
- clipPlanePoints.UpperRight = pos + transform.right * width;
- clipPlanePoints.UpperRight += transform.up * height;
- clipPlanePoints.UpperRight += transform.forward * distance;
- clipPlanePoints.UpperLeft = pos - transform.right * width;
- clipPlanePoints.UpperLeft += transform.up * height;
- clipPlanePoints.UpperLeft += transform.forward * distance;
- return clipPlanePoints;
- }
- private struct ClipPlanePoints
- {
- public Vector3 UpperLeft;
- public Vector3 LowerLeft;
- public Vector3 UpperRight;
- public Vector3 LowerRight;
- }
- private void OnCollisionEnter(Collision c)
- {
- if(c.gameObject.tag == "Land")
- {
- Grounded++;
- }
- }
- private void OnCollisionExit(Collision c)
- {
- if(c.gameObject.tag == "Land" && Grounded > 0)
- {
- Grounded--;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement