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
- {
- //Move speed of player
- public float moveSpeed;
- //Animator variable, used to trigger animations
- private Animator anim;
- private Rigidbody2D myRigidbody;
- //Movement related stuff, playerMoving to indicate is player moving, lastMove to indicate when player last moved
- public bool playerMoving;
- private Vector2 lastMove;
- private static bool playerExists;
- public bool canMove;
- // Start is called before the first frame update
- void Start()
- {
- //Get the animator for the script
- anim = GetComponent<Animator>();
- myRigidbody = GetComponent<Rigidbody2D>();
- //Prevent multiple players when scene hopping
- if (!playerExists)
- {
- playerExists = true;
- DontDestroyOnLoad(transform.gameObject);
- }
- else
- {
- Destroy(gameObject);
- }
- }
- // Update is called once per frame
- void Update()
- {
- if (canMove && playerMoving)
- {
- //Trigger animations to face on the XY axis
- anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));
- anim.SetFloat("MoveY", Input.GetAxisRaw("Vertical"));
- }
- if (!canMove)
- {
- myRigidbody.velocity = Vector2.zero;
- return;
- }
- //Put playermoving to false so no animations go off other than idle
- playerMoving = false;
- if (canMove)
- {
- //Script to make the game understand WASD + KEYS
- if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)
- {
- //Script to make the game understand movement * moveSpeed * time.deltatime to make accurate frame moving
- //transform.Translate(new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f));
- myRigidbody.velocity = new Vector2(Input.GetAxisRaw("Horizontal") * moveSpeed, myRigidbody.velocity.y);
- //Make sure the variable is true to trigger animation
- playerMoving = true;
- //Make the game remember your last movement direction
- lastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
- }
- //Repeat script for Y axis
- if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f)
- {
- //transform.Translate(new Vector3(0f, Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime, 0f));
- myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, Input.GetAxisRaw("Vertical") * moveSpeed);
- playerMoving = true;
- lastMove = new Vector2(0f, Input.GetAxisRaw("Vertical"));
- }
- if (Input.GetAxisRaw("Horizontal") < 0.5f && Input.GetAxisRaw("Horizontal") > -0.5f)
- {
- myRigidbody.velocity = new Vector2(0f, myRigidbody.velocity.y);
- }
- if (Input.GetAxisRaw("Vertical") < 0.5f && Input.GetAxisRaw("Vertical") > -0.5f)
- {
- myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, 0f);
- }
- }
- anim.SetBool("PlayerMoving", playerMoving);
- anim.SetFloat("LastMoveX", lastMove.x);
- anim.SetFloat("LastMoveY", lastMove.y);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement