Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Events;
- public class Movement : MonoBehaviour
- {
- //variables
- public Animator anim; // sets variable for animator
- private bool movingleft; //anim bool
- private bool movingright;//anim bool
- private bool movingup;//anim bool
- private bool movingdown;//anim bool
- public float movespeed; //movement speed slider
- //edited by dlich
- public float movementSpeed = 1f;
- private Vector2 oldPos;
- private Vector2 newpos;// new position for movement destination
- private float lerpValue = 0;
- public enum State{idle, movement};
- public State state = State.idle;
- void Start() //start of game trigger
- {
- anim = GetComponent<Animator>(); //get the animator for the player object
- }
- void FixedUpdate() //fixed update runs in sync with the physics machine, which keeps fps good
- {
- if(state == State.idle)
- {
- if(HandleInput())
- {
- lerpValue = 0;
- oldPos = transform.position;
- state = State.movement;
- }
- }
- if(state == State.movement)
- {
- if(lerpValue < 1)
- {
- transform.position = Vector2.Lerp(oldPos, newpos, lerpValue);
- lerpValue += Time.deltatime * movementSpeed
- }
- else state = State.idle;
- }
- HandleAnimation();
- }
- //added by dlich
- public bool HandleMovement()
- {
- newpos = transform.position;
- if (Input.GetKeyDown(KeyCode.W))//move up
- {
- newpos.y = newpos.y + 5; // increments y by 5 (moving up by 5)
- return true;
- }
- if (Input.GetKeyDown(KeyCode.A))// move left
- {
- newpos.x = newpos.x - 5;
- return true;
- }
- if (Input.GetKeyDown(KeyCode.D))// move right
- {
- newpos.x = newpos.x + 5;
- return true;
- }
- if (Input.GetKeyDown(KeyCode.S)) // move down
- {
- newpos.y = newpos.y - 5;
- return true;
- }
- return false;
- }
- ////////////////////////////////////////////////////////////////
- ////Animation
- ////////////////////////////////////////////////////////////////
- HandleAnimation()
- {
- movingleft = Input.GetKey(KeyCode.A);
- movingright = Input.GetKey(KeyCode.D);
- movingup = Input.GetKey(KeyCode.W);
- movingdown = Input.GetKey(KeyCode.S);
- if (movingleft)
- {
- anim.SetBool("moving_left", true);
- }
- if (!movingleft)
- {
- anim.SetBool("moving_left", false);
- }
- //
- if (movingright)
- {
- anim.SetBool("moving_right", true);
- }
- if (!movingright)
- {
- anim.SetBool("moving_right", false);
- }
- //
- if (movingup)
- {
- anim.SetBool("moving_up", true);
- }
- if (!movingup)
- {
- anim.SetBool("moving_up", false);
- }
- //
- if (movingdown)
- {
- anim.SetBool("moving_down", true);
- }
- if (!movingdown)
- {
- anim.SetBool("moving_down", false);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement