Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerController : MonoBehaviour
- {
- public float walkSpeed = 3f;
- public float runSpeed = 5f;
- private float speed = 3f;
- private Vector2 lastMove;
- private bool _isMoving;
- private bool _isRunning;
- public bool _isPlayingWalkParticle;
- public static bool _canMove;
- public ParticleSystem playerWalkParticle;
- //public ParticleSystem playerWalkParticleRight;
- private Animator anim;
- private Rigidbody2D rb2d;
- void Awake()
- {
- _canMove = true;
- }
- void Start()
- {
- anim = gameObject.GetComponent<Animator>();
- rb2d = gameObject.GetComponent<Rigidbody2D>();
- playerWalkParticle = gameObject.transform.Find("PlayerParticlesEffectsManager").Find("PlayerWalkEffect").GetComponent<ParticleSystem>();
- }
- private void Update()
- {
- }
- // Update is called once per frame
- void FixedUpdate()
- {
- Vector2 movementVector = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
- speed = walkSpeed;
- //maxSpeed = speed;
- float input_x = Input.GetAxisRaw("Horizontal");
- float input_y = Input.GetAxisRaw("Vertical");
- //bool isMoving = (Mathf.Abs(input_x) + Mathf.Abs(input_y)) > 0;
- anim.SetBool("isMoving", isMoving);
- if (_canMove && isMoving)
- {
- anim.SetFloat("MoveX", input_x);
- anim.SetFloat("MoveY", input_y);
- rb2d.MovePosition(rb2d.position + movementVector * speed * Time.smoothDeltaTime);
- //transform.position += new Vector3(input_x, input_y, 0).normalized * speed * Time.deltaTime;
- //rb2d.velocity = new Vector2(Mathf.Lerp(0, input_x * speed, 0.8f),Mathf.Lerp(0, input_y * speed, 0.8f)).normalized;
- lastMove = new Vector2(0f, Input.GetAxisRaw("Vertical"));
- anim.SetFloat("lastMoveY", lastMove.y);
- lastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
- anim.SetFloat("lastMoveX", lastMove.x);
- }
- else
- {
- rb2d.velocity = Vector2.zero;
- anim.SetBool("isMoving", false);
- }
- }
- private void LateUpdate()
- {
- var em = playerWalkParticle.emission;
- if (Input.GetKey(KeyCode.LeftShift) && !_isRunning)
- {
- speed = runSpeed;
- anim.speed = 2;
- em.rateOverTime = 4;
- _isRunning = true;
- }
- else if (!Input.GetKey(KeyCode.LeftShift) && _isRunning)
- {
- speed = walkSpeed;
- anim.speed = 1;
- em.rateOverTime = 2;
- _isRunning = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement