Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.99 KB | None | 0 0
  1. public class FPScontroller : MonoBehaviour {
  2.  
  3. // Should this script respond to input?
  4. public bool canControl = true;
  5. public GameObject lookObj; //This is root object that containc MainCamera, Weapons etc.
  6. public GameObject joystick;
  7. bool useFixedUpdate = false;
  8.  
  9.  
  10. //Check when run, walk or when can run or not
  11. [HideInInspector]
  12. public bool Running ;
  13. [HideInInspector]
  14. public bool Walking;
  15. [HideInInspector]
  16. public bool canRun;
  17. [HideInInspector]
  18. public Vector3 rorationDir;
  19.  
  20. //Ladder variables
  21. private GameObject mainCamera = null;
  22. [HideInInspector]
  23. public bool onLadder = false;
  24. //private float ladderHopSpeed = 6.0f;
  25.  
  26. // For the next variables, @System.NonSerialized tells Unity to not serialize the variable or show it in the inspector view.
  27. // Very handy for organization!
  28.  
  29. // The current global direction we want the character to move in.
  30. [System.NonSerialized]
  31. public Vector3 inputMoveDirection = Vector3.zero;
  32.  
  33. // Is the jump button held down? We use this interface instead of checking
  34. // for the jump button directly so this script can also be used by AIs.
  35. [System.NonSerialized]
  36. public bool inputJump = false;
  37.  
  38. [HideInInspector]
  39. public bool inputRun = false;
  40.  
  41. [HideInInspector]
  42. public bool inputCrouch = false;
  43.  
  44. [HideInInspector]
  45. public bool inputProne = false;
  46.  
  47. [System.Serializable]
  48. public class FPScontrollerMovement {
  49. // The maximum horizontal speed when moving
  50. [HideInInspector]
  51. public float maxForwardSpeed = 10.0f;
  52. [HideInInspector]
  53. public float maxSidewaysSpeed = 10.0f;
  54. [HideInInspector]
  55. public float maxBackwardsSpeed = 10.0f;
  56.  
  57. //Run and walk variables
  58. public float WalkSpeed = 6.0f;
  59. public float RunSpeed = 9.0f;
  60. //Crouch
  61. public bool canCrouch = true;
  62. public float CrouchSpeed = 3.0f;
  63. public float crouchHeight = 1.5f;
  64. public float crouchSmooth = 8;
  65. //prone
  66. public bool canProne = true;
  67. public float ProneSpeed = 1.5f;
  68. public float proneHeight = 0.7f;
  69.  
  70.  
  71.  
  72. // Curve for multiplying speed based on slope (negative = downwards)
  73. public AnimationCurve slopeSpeedMultiplier = new AnimationCurve(new Keyframe(-90, 1), new Keyframe(0, 1), new Keyframe(90, 0));
  74.  
  75. // How fast does the character change speeds? Higher is faster.
  76. public float maxGroundAcceleration = 30.0f;
  77. public float maxAirAcceleration = 20.0f;
  78.  
  79. // The gravity for the character
  80. public float gravity = 10.0f;
  81. public float maxFallSpeed = 20.0f;
  82.  
  83. [HideInInspector]
  84. public bool enableGravity = true;
  85.  
  86. // For the next variables, @System.NonSerialized tells Unity to not serialize the variable or show it in the inspector view.
  87. // Very handy for organization!
  88.  
  89. // The last collision flags returned from controller.Move
  90. [System.NonSerialized]
  91. public CollisionFlags collisionFlags;
  92.  
  93. // We will keep track of the character's current velocity,
  94. [System.NonSerialized]
  95. public Vector3 velocity;
  96.  
  97. // This keeps track of our current velocity while we're not grounded
  98. [System.NonSerialized]
  99. public Vector3 frameVelocity = Vector3.zero;
  100.  
  101. [System.NonSerialized]
  102. public Vector3 hitPoint = Vector3.zero;
  103.  
  104. [System.NonSerialized]
  105. public Vector3 lastHitPoint = new Vector3(Mathf.Infinity, 0, 0);
  106. }
  107. public FPScontrollerMovement movement = new FPScontrollerMovement();
  108.  
  109. void Awake () {
  110. if (GetComponent<NetworkView> ().isMine) {
  111.  
  112. joystick = GameObject.Find ("Joystick");
  113. controller = gameObject.GetComponent<CharacterController>();
  114. standartHeight = controller.height;
  115. /*if(GameObject.FindWithTag("LookObject") != null){
  116. lookObj = GameObject.FindWithTag("LookObject");
  117. }*/
  118. centerY = controller.center.y;
  119. tr = transform;
  120.  
  121. canRun = true;
  122. canStand = true;
  123. StartCoroutine(setupBools());
  124.  
  125. }
  126. }
  127.  
  128. void Update () {
  129. if (GetComponent<NetworkView> ().isMine) {
  130. if (!useFixedUpdate) {
  131. UpdateFunction ();
  132. }
  133.  
  134. movement.velocity.x = joystick.GetComponent<VirtualJoystick> ().Horizontal () * 5f;
  135. movement.velocity.z = joystick.GetComponent<VirtualJoystick> ().Vertical () * 5f;
  136.  
  137.  
  138.  
  139. //Run input
  140. if (Input.GetAxis ("Vertical") > 0.1f && inputRun && canRun && !onLadder && Walking) {
  141. if (canStand && canStandCrouch) {
  142. OnRunning ();
  143. }
  144. } else {
  145. OffRunning ();
  146. }
  147.  
  148. //Check when walk or not
  149. if ((movement.velocity.x > 0.01f || movement.velocity.z > 0.01f) || (movement.velocity.x < -0.01f || movement.velocity.z < -0.01f)) {
  150. RunAnimation1 ();
  151. Debug.Log ("Forward");
  152. Walking = true;
  153. }else if (movement.velocity.x > 0.01f) {
  154. Walking = true;
  155. Debug.Log ("Right");
  156. } else if (movement.velocity.x < -0.01f) {
  157. Walking = true;
  158. Debug.Log ("Left");
  159. } else {
  160. RunAnimation ();
  161. Walking = false;
  162. }
  163.  
  164.  
  165. if (!canControl)
  166. return;
  167.  
  168. if (movement.canCrouch) {
  169. if (!onLadder) {
  170. Crouch ();
  171. }
  172. }
  173.  
  174. if (movement.canProne) {
  175. if (!onLadder) {
  176. Prone ();
  177. }
  178. }
  179.  
  180. if (onLadder) {
  181. grounded = false;
  182. crouch = false;
  183. prone = false;
  184. }
  185.  
  186. if (!crouch && !prone && controller.height < standartHeight - 0.01f) {
  187. controller.height = Mathf.Lerp (controller.height, standartHeight, Time.deltaTime / movement.crouchSmooth);
  188. controller.center = new Vector3 (controller.center.x, Mathf.Lerp (controller.center.y, centerY, Time.deltaTime / movement.crouchSmooth), controller.center.z);
  189. lookObj.transform.localPosition = new Vector3 (lookObj.transform.localPosition.x, Mathf.Lerp (lookObj.transform.localPosition.y, standartHeight, Time.deltaTime / movement.crouchSmooth), lookObj.transform.localPosition.z);
  190. }
  191. }
  192. }
  193.  
  194. void RunAnimation(){
  195. GetComponent<NetworkView> ().RPC ("SysnAnimation", RPCMode.All, 0);
  196. }
  197. void RunAnimation1(){
  198. GetComponent<NetworkView> ().RPC ("SysnAnimation", RPCMode.All, 1);
  199. }
  200. void RunAnimation2(){
  201. GetComponent<NetworkView> ().RPC ("SysnAnimation", RPCMode.All, 2);
  202. }
  203.  
  204. [RPC]
  205. void SysnAnimation(int index){
  206. if (index == 0) {
  207. GetComponent<Animator> ().Play ("Idle Aim");
  208. } else if (index == 1) {
  209. GetComponent<Animator> ().Play ("Walk Aiming");
  210. } else if (index == 2) {
  211. GetComponent<Animator> ().Play ("Jump");
  212. }
  213. }
  214.  
  215. void OnRunning (){
  216. Debug.Log ("Run");
  217. Running = true;
  218. movement.maxForwardSpeed = movement.RunSpeed;
  219. movement.maxSidewaysSpeed = movement.RunSpeed;
  220. //Make bigger extra height when player run to increase jump distance
  221. jumping.extraHeight = jumping.baseHeight + 0.15f;
  222. }
  223.  
  224. void OffRunning (){
  225. Running = false;
  226. if(crouch || prone)
  227. return;
  228. movement.maxForwardSpeed = movement.WalkSpeed;
  229. movement.maxSidewaysSpeed = movement.WalkSpeed;
  230. movement.maxBackwardsSpeed = movement.WalkSpeed/2;
  231. //Change extraheight value to default when player walk
  232. jumping.extraHeight = jumping.baseHeight;
  233. }}
  234.  
  235. public class VirtualJoystick : MonoBehaviour, IDragHandler, IPointerUpHandler,IPointerDownHandler {
  236.  
  237.  
  238. private Image bgImg;
  239. private Image JoyStickImage;
  240. private Vector3 InputVector;
  241.  
  242. private void Start(){
  243. bgImg = GetComponent<Image> ();
  244. JoyStickImage = transform.GetChild (0).GetComponent<Image> ();
  245.  
  246. }
  247.  
  248. public virtual void OnDrag(PointerEventData ped){
  249. Vector2 pos;
  250. if (RectTransformUtility.ScreenPointToLocalPointInRectangle (bgImg.rectTransform, ped.position, ped.pressEventCamera, out pos)) {
  251. pos.x = (pos.x / bgImg.rectTransform.sizeDelta.x);
  252. pos.y = (pos.y / bgImg.rectTransform.sizeDelta.y);
  253.  
  254. InputVector = new Vector3 (pos.x * 2 + 1, 0, pos.y * 2 - 1);
  255. InputVector = (InputVector.magnitude > 1) ? InputVector.normalized : InputVector;
  256.  
  257. JoyStickImage.rectTransform.anchoredPosition = new Vector3 (InputVector.x * (bgImg.rectTransform.sizeDelta.x / 2.5f),
  258. InputVector.z * (bgImg.rectTransform.sizeDelta.y / 2.5f));
  259.  
  260. Debug.Log (InputVector);
  261. }
  262. }
  263.  
  264. public virtual void OnPointerDown(PointerEventData ped){
  265.  
  266. OnDrag (ped);
  267. }
  268.  
  269. public virtual void OnPointerUp(PointerEventData ped){
  270.  
  271. InputVector = Vector3.zero;
  272. JoyStickImage.rectTransform.anchoredPosition = Vector3.zero;
  273. }
  274.  
  275. public float Horizontal(){
  276.  
  277. if (InputVector.x != 0)
  278. return InputVector.x;
  279. else
  280. return Input.GetAxis ("Horizontal");
  281.  
  282. }
  283. public float Vertical(){
  284.  
  285. if (InputVector.z != 0)
  286. return InputVector.z;
  287. else
  288. return Input.GetAxis ("Vertical");
  289.  
  290. }
  291.  
  292. public class SwipeCam : MonoBehaviour {
  293.  
  294. private Vector3 firstPoint;
  295. private Vector3 secondPoint;
  296. private float xAngle = 0.0f;
  297. private float yAngle = 0.0f;
  298. private float xAngleTemp = 0.0f;
  299. private float yAngleTemp = 0.0f;
  300.  
  301. void Start(){
  302. xAngle = 0.0f;
  303. yAngle = 0.0f;
  304. this.transform.rotation = Quaternion.Euler (yAngle, xAngle, 0.0f);
  305.  
  306. }
  307.  
  308. void Update(){
  309.  
  310.  
  311. if (Input.touchCount > 0) {
  312.  
  313. for (int i = 0; i < Input.touchCount; i++) {
  314.  
  315. Touch touch = Input.GetTouch (i);
  316.  
  317. if (touch.position.x > Screen.width / 2) {
  318.  
  319. if (touch.phase == TouchPhase.Began) {
  320.  
  321. firstPoint = Input.GetTouch (0).position;
  322. xAngleTemp = xAngle;
  323. yAngleTemp = yAngle;
  324. }
  325. if (touch.phase == TouchPhase.Moved) {
  326. secondPoint = Input.GetTouch (0).position;
  327.  
  328. xAngle = xAngleTemp + (secondPoint.x - firstPoint.x) * 180.0f / Screen.width;
  329. yAngle = yAngleTemp + (secondPoint.y - firstPoint.y) * 180.0f / -Screen.height;
  330.  
  331. yAngle = Mathf.Clamp (yAngle, -30f, 30f);
  332.  
  333.  
  334. this.transform.rotation = Quaternion.Euler (yAngle, xAngle, 0.0f);
  335. this.gameObject.GetComponentInParent<FPScontroller> ().transform.rotation = Quaternion.Euler (0.0f, xAngle, 0.0f);
  336. }
  337.  
  338. }
  339. }
  340. }
  341.  
  342.  
  343. }
  344. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement