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 TopDownPlayer : MonoBehaviour
- {
- public float speed = 3f;
- private Rigidbody2D rb;
- private Animator animator;
- private SpriteRenderer sp;
- private Vector2 moveDirection, lastMoveDirection;
- // Start is called before the first frame update
- void Start()
- {
- rb = GetComponent<Rigidbody2D>();
- animator = GetComponent<Animator>();
- sp = GetComponent<SpriteRenderer>();
- }
- // Update is called once per frame
- void Update()
- {
- moveDirection.x = Input.GetAxisRaw(Constants.HorizontalInput);
- moveDirection.y = Input.GetAxisRaw(Constants.VerticalInput);
- moveDirection = moveDirection.normalized;
- animator.SetFloat("Speed", rb.velocity.sqrMagnitude);
- if (moveDirection.sqrMagnitude > 0f)
- {
- lastMoveDirection = moveDirection;
- }
- animator.SetFloat("X", lastMoveDirection.x);
- animator.SetFloat("Y", lastMoveDirection.y);
- sp.flipX = lastMoveDirection.x < 0f;
- }
- private void FixedUpdate()
- {
- rb.velocity = moveDirection * speed;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement