Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class PlayerControl : Photon.MonoBehaviour
- {
- private static int playerCount = 0;
- public static int PlayerCount
- {
- get
- {
- return playerCount;
- }
- }
- public float rotationSpeed;
- private Vector3 lastVelocity;
- private Vector3 currentPosition;
- private Quaternion currentRotation;
- public OnJoinedInstantiate BubbleSpawner;
- public enum HorizontalRestriction { None, LeftOnly, RightOnly };
- public enum VerticalRestriction { None, UpOnly, DownOnly };
- public HorizontalRestriction HorizontalRestr;
- public VerticalRestriction VerticalRestr;
- private CharacterController charc;
- public GameObject popParticle;
- private PhotonView myPhotonView;
- public int sprintCapacity;
- private int maxSprintCapacity;
- private CameraFollowMe camFollow;
- public float speed;
- public float sprintSpeedAdd = 0;
- private float addSpeed;
- private float baseSpeed;
- private bool sprintRefiled;
- void Start()
- {
- playerCount++;
- if (!photonView.isMine)
- {
- GetComponent<Rigidbody>().isKinematic = true;
- }
- charc = GetComponent<CharacterController>();
- if (BubbleSpawner == null)
- BubbleSpawner = GameObject.Find("Scripts").GetComponent<OnJoinedInstantiate>();
- BubbleSpawner.players.Add(gameObject.GetComponent<PhotonView>());
- camFollow = Camera.main.GetComponent<CameraFollowMe>();
- baseSpeed = speed;
- maxSprintCapacity = sprintCapacity;
- sprintCapacity = 0;
- myPhotonView = gameObject.GetComponent<PhotonView>();
- }
- // Update is called once per frame
- void Update()
- {
- if (!photonView.isMine)
- {
- transform.position = Vector3.Slerp(transform.position, currentPosition, Time.deltaTime * 2);
- transform.rotation = Quaternion.Slerp(transform.rotation, currentRotation, Time.deltaTime * 2);
- }
- else
- {
- getInput();
- }
- }
- public void AddKill()
- {
- int kills = 0;
- if (myPhotonView.owner.customProperties.ContainsKey("kills"))
- kills = (int)myPhotonView.owner.customProperties["kills"];
- kills++;
- myPhotonView.owner.SetCustomProperties(new ExitGames.Client.Photon.Hashtable() { { "kills", kills } });
- }
- public int GetKills()
- {
- int kills = 0;
- if (myPhotonView.owner.customProperties.ContainsKey("kills"))
- kills = (int)myPhotonView.owner.customProperties["kills"];
- return kills;
- }
- [RPC]
- void die()
- {
- PhotonNetwork.Destroy(gameObject);
- if (BubbleSpawner == null)
- BubbleSpawner = GameObject.Find("Scripts").GetComponent<OnJoinedInstantiate>();
- }
- void OnDestroy()
- {
- CreatePopParticle();
- /*
- if (BubbleSpawner == null)
- BubbleSpawner = GameObject.Find("Scripts").GetComponent<OnJoinedInstantiate>();
- */
- //BubbleSpawner.SpawnWithDelay(1.5f);
- }
- void getInput()
- {
- Vector3 moveDirection = transform.forward;
- float yawMouse = Input.GetAxis("Mouse X");
- float pitchMouse = Input.GetAxis("Mouse Y");
- Vector3 targetFlyRotation = Vector3.zero;
- Screen.lockCursor = true;
- if (Mathf.Abs(yawMouse) > 0.1f || Mathf.Abs(pitchMouse) > 0.1f)
- {
- targetFlyRotation = yawMouse * transform.right + pitchMouse * transform.up;
- targetFlyRotation.Normalize();
- targetFlyRotation *= Time.deltaTime * 3.0f;
- //limit x rotation if looking too much up or down
- //Log out the limitX value for this to make sense
- float limitX = Quaternion.LookRotation(moveDirection + targetFlyRotation).eulerAngles.x;
- //70 sets the rotation limit in the down direction
- //290 sets limit for up direction
- if (limitX < 90 && limitX > 70 || limitX > 270 && limitX < 290)
- {
- }
- else
- {
- moveDirection += targetFlyRotation;
- //does the actual rotation on the object if no limits are breached
- transform.rotation = Quaternion.LookRotation(moveDirection);
- }
- }
- float Vertical = Input.GetAxis("Vertical");
- float Horizontal = Input.GetAxis("Horizontal");
- Vector3 moveDir = transform.forward * Vertical + transform.right * Horizontal;
- if (Input.GetKey(KeyCode.Space) && sprintCapacity > 0 && sprintRefiled)
- {
- addSpeed = sprintSpeedAdd;
- camFollow.preferedDistance = 3f;
- sprintCapacity--;
- }
- else
- {
- sprintRefiled = false;
- camFollow.preferedDistance = 2f;
- addSpeed = 0f;
- if (sprintCapacity < maxSprintCapacity)
- sprintCapacity++;
- else
- sprintCapacity = maxSprintCapacity;
- if (sprintCapacity == maxSprintCapacity)
- sprintRefiled = true;
- }
- speed = Mathf.Lerp(speed, baseSpeed + addSpeed, Time.deltaTime);
- charc.Move(moveDir * Time.deltaTime * speed);
- }
- void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
- {
- if (stream.isWriting)
- {
- stream.SendNext(transform.position);
- stream.SendNext(transform.rotation);
- }
- else
- {
- currentPosition = (Vector3)stream.ReceiveNext();
- currentRotation = (Quaternion)stream.ReceiveNext();
- }
- }
- void OnTriggerEnter(Collider coll)
- {
- if (coll.gameObject.tag == "Spike" && coll.gameObject.transform.parent != gameObject)
- {
- PlayerControl pc = coll.gameObject.transform.parent.gameObject.GetComponent<PlayerControl>();
- if (pc != null)
- {
- pc.AddKill();
- }
- myPhotonView.RPC("die", PhotonTargets.All);
- }
- }
- [RPC]
- public void CreatePopParticle()
- {
- GameObject go = Instantiate(popParticle, transform.position, Quaternion.identity) as GameObject;
- Destroy(go, 1.5f);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement