Skip to content

Instantly share code, notes, and snippets.

@Dysp
Created March 31, 2016 17:17
Show Gist options
  • Save Dysp/4ec7c7d49eb6ef65d0f5fdafb7a88b93 to your computer and use it in GitHub Desktop.
Save Dysp/4ec7c7d49eb6ef65d0f5fdafb7a88b93 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
Rigidbody2D rbody;
Animator anim;
public float maxSpeed = 10;
public float walkSpeed = 2;
private float curSpeed;
void Start () {
rbody = GetComponent<Rigidbody2D> ();
anim = GetComponent<Animator> ();
// var runSpeed = walkSpeed * 2;
}
void FixedUpdate () {
//Håndterer input
var input_x = Input.GetAxisRaw ("Horizontal");
var input_y = Input.GetAxisRaw ("Vertical");
Vector2 movement_vector = new Vector2 (input_x, input_y);
//Giver animator informationer om bevægelse
if (movement_vector != Vector2.zero) {
anim.SetBool ("is_walking", true);
anim.SetFloat ("input_x", movement_vector.x);
anim.SetFloat ("input_y", movement_vector.y);
} else {
anim.SetBool ("is_walking", false);
}
curSpeed = walkSpeed;
//Håndterer bevægelse
rbody.velocity = new Vector2 (Mathf.Lerp (0, input_x * curSpeed, 0.8f), Mathf.Lerp (0, input_y * curSpeed, 0.8f));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment