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 CharacterMovement : MonoBehaviour {
- public GameObject player;
- // public float speed = 0.05f; // orginal speed of player
- public float speed = 0.1f; // I was told that the speed seemed a bit to slow, especially for a wilderness exploration game
- private bool up, down, left, right;
- // Use this for initialization
- void Start() {
- up = false;
- down = false;
- left = false;
- right = false;
- }
- // Update is called once per frame
- void Update() {
- up = Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow) ? true : false;
- down = Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow) ? true : false;
- left = Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow) ? true : false;
- right = Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow) ? true : false;
- }
- private void FixedUpdate()
- {
- if (up || down || left || right)
- player.transform.Translate((boolToInt(right) - boolToInt(left)) * speed, (boolToInt(up) - boolToInt(down)) * speed, 0);
- }
- public int boolToInt(bool input)
- {
- return input ? 1 : 0;
- }
- }
- ============================================================================================================================
- /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*\
- | |
- | Made by Jason Helman © Jason Tech And Games |
- | I do not condone the use of my work without my permission |
- | |
- \*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
- using UnityEngine;
- public class PlayerMovementController : MonoBehaviour
- {
- public GameObject player;
- [System.NonSerialized]
- public float playerSpeed = 10f;
- private int right = 0;
- private int up = 0;
- private void FixedUpdate()
- {
- right = 0;
- up = 0;
- right += Input.GetKey("d") ? 1 : 0;
- right -= Input.GetKey("a") ? 1 : 0;
- up += Input.GetKey("w") ? 1 : 0;
- up -= Input.GetKey("s") ? 1 : 0;
- player.transform.position += (Vector3)new Vector2(right, up) * Time.deltaTime * playerSpeed;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement