Advertisement
Guest User

Untitled

a guest
Dec 7th, 2017
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerController : MonoBehaviour
  6. {
  7.  
  8. public float walkSpeed = 3f;
  9. public float runSpeed = 5f;
  10. private float speed = 3f;
  11.  
  12. private Vector2 lastMove;
  13.  
  14. private bool _isMoving;
  15. private bool _isRunning;
  16. public bool _isPlayingWalkParticle;
  17. public static bool _canMove;
  18. public ParticleSystem playerWalkParticle;
  19. //public ParticleSystem playerWalkParticleRight;
  20.  
  21. private Animator anim;
  22. private Rigidbody2D rb2d;
  23.  
  24. void Awake()
  25. {
  26.  
  27. _canMove = true;
  28.  
  29. }
  30.  
  31. void Start()
  32. {
  33.  
  34. anim = gameObject.GetComponent<Animator>();
  35. rb2d = gameObject.GetComponent<Rigidbody2D>();
  36. playerWalkParticle = gameObject.transform.Find("PlayerParticlesEffectsManager").Find("PlayerWalkEffect").GetComponent<ParticleSystem>();
  37. }
  38.  
  39. private void Update()
  40. {
  41.  
  42.  
  43. }
  44.  
  45. // Update is called once per frame
  46. void FixedUpdate()
  47. {
  48.  
  49. Vector2 movementVector = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
  50. speed = walkSpeed;
  51. //maxSpeed = speed;
  52. float input_x = Input.GetAxisRaw("Horizontal");
  53. float input_y = Input.GetAxisRaw("Vertical");
  54.  
  55. //bool isMoving = (Mathf.Abs(input_x) + Mathf.Abs(input_y)) > 0;
  56. anim.SetBool("isMoving", isMoving);
  57.  
  58. if (_canMove && isMoving)
  59. {
  60. anim.SetFloat("MoveX", input_x);
  61. anim.SetFloat("MoveY", input_y);
  62.  
  63. rb2d.MovePosition(rb2d.position + movementVector * speed * Time.smoothDeltaTime);
  64. //transform.position += new Vector3(input_x, input_y, 0).normalized * speed * Time.deltaTime;
  65. //rb2d.velocity = new Vector2(Mathf.Lerp(0, input_x * speed, 0.8f),Mathf.Lerp(0, input_y * speed, 0.8f)).normalized;
  66.  
  67. lastMove = new Vector2(0f, Input.GetAxisRaw("Vertical"));
  68. anim.SetFloat("lastMoveY", lastMove.y);
  69. lastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
  70. anim.SetFloat("lastMoveX", lastMove.x);
  71.  
  72. }
  73. else
  74. {
  75. rb2d.velocity = Vector2.zero;
  76. anim.SetBool("isMoving", false);
  77.  
  78. }
  79.  
  80. }
  81.  
  82. private void LateUpdate()
  83. {
  84. var em = playerWalkParticle.emission;
  85. if (Input.GetKey(KeyCode.LeftShift) && !_isRunning)
  86. {
  87. speed = runSpeed;
  88. anim.speed = 2;
  89. em.rateOverTime = 4;
  90. _isRunning = true;
  91. }
  92. else if (!Input.GetKey(KeyCode.LeftShift) && _isRunning)
  93. {
  94. speed = walkSpeed;
  95. anim.speed = 1;
  96. em.rateOverTime = 2;
  97. _isRunning = false;
  98. }
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement