Skip to content

Instantly share code, notes, and snippets.

@Arumksh
Created July 29, 2018 02:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Arumksh/7b69cb8d909cdfe5555895e905615682 to your computer and use it in GitHub Desktop.
Save Arumksh/7b69cb8d909cdfe5555895e905615682 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
[RequireComponent(typeof(Animator))]
public class PlayerController : MonoBehaviour {
public Vector2 lastMove;
[SerializeField]
private float moveSpeed;
private Rigidbody2D rb;
private Animator animator;
private Vector2 movement;
void Start () {
rb = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
}
void Update()
{
movement = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
Animate();
}
private void FixedUpdate()
{
MovePlayer();
}
private void MovePlayer()
{
rb.AddForce(movement.normalized * moveSpeed * 1.5f);
}
public void Animate()
{
if (Mathf.Abs(movement.x) > 0.5f)
{
lastMove.x = movement.x;
lastMove.y = 0;
}
if (Mathf.Abs(movement.y) > 0.5f)
{
lastMove.y = movement.y;
lastMove.x = 0;
}
animator.SetFloat("Dir_X", movement.x);
animator.SetFloat("Dir_Y", movement.y);
animator.SetFloat("LastMove_X", lastMove.x);
animator.SetFloat("LastMove_Y", lastMove.y);
animator.SetFloat("Input", movement.magnitude);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment