Advertisement
JJCUBER

Untitled

Dec 2nd, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.38 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CharacterMovement : MonoBehaviour {
  6.  
  7.     public GameObject player;
  8.     // public float speed = 0.05f; // orginal speed of player
  9.     public float speed = 0.1f; // I was told that the speed seemed a bit to slow, especially for a wilderness exploration game
  10.     private bool up, down, left, right;
  11.  
  12.     // Use this for initialization
  13.     void Start() {
  14.         up = false;
  15.         down = false;
  16.         left = false;
  17.         right = false;
  18.     }
  19.  
  20.     // Update is called once per frame
  21.     void Update() {
  22.         up = Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow) ? true : false;
  23.         down = Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow) ? true : false;
  24.         left = Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow) ? true : false;
  25.         right = Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow) ? true : false;
  26.     }
  27.  
  28.     private void FixedUpdate()
  29.     {
  30.         if (up || down || left || right)
  31.             player.transform.Translate((boolToInt(right) - boolToInt(left)) * speed, (boolToInt(up) - boolToInt(down)) * speed, 0);
  32.     }
  33.  
  34.     public int boolToInt(bool input)
  35.     {
  36.         return input ? 1 : 0;
  37.     }
  38. }
  39.  
  40.  
  41. ============================================================================================================================
  42.  
  43.  
  44. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*\
  45. |                                                               |
  46. |          Made by Jason Helman © Jason Tech And Games          |
  47. |   I do not condone the use of my work without my permission   |
  48. |                                                               |
  49. \*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  50.  
  51. using UnityEngine;
  52.  
  53. public class PlayerMovementController : MonoBehaviour
  54. {
  55.     public GameObject player;
  56.     [System.NonSerialized]
  57.     public float playerSpeed = 10f;
  58.  
  59.     private int right = 0;
  60.     private int up = 0;
  61.  
  62.     private void FixedUpdate()
  63.     {
  64.         right = 0;
  65.         up = 0;
  66.  
  67.         right += Input.GetKey("d") ? 1 : 0;
  68.         right -= Input.GetKey("a") ? 1 : 0;
  69.  
  70.         up += Input.GetKey("w") ? 1 : 0;
  71.         up -= Input.GetKey("s") ? 1 : 0;
  72.  
  73.         player.transform.position += (Vector3)new Vector2(right, up) * Time.deltaTime * playerSpeed;
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement