Archeia

8 Directional Movement WASD for Unity

Sep 21st, 2014
355
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Move : MonoBehaviour {
  5.  
  6.     float speed = 7f;
  7.  
  8.     // Use this for initialization
  9.     void Start () {
  10.    
  11.     }
  12.    
  13.     // Update is called once per frame
  14.     void Update () {
  15.    
  16.         //Basic Movement
  17.  
  18.         if (Input.GetKey(KeyCode.W))
  19.         {
  20.         // Make stuff work here.
  21.             transform.Translate(0f, speed * Time.deltaTime,0f); //x,y,z
  22.         }
  23.  
  24.         if (Input.GetKey(KeyCode.S))
  25.         {
  26.             // Make stuff work here.
  27.             transform.Translate(0f, -speed * Time.deltaTime,0f); //x,y,z
  28.            
  29.         }
  30.  
  31.         if (Input.GetKey(KeyCode.A))
  32.         {
  33.             // Make stuff work here.
  34.             transform.Translate(-speed * Time.deltaTime, 0f,0f); //x,y,z
  35.            
  36.         }
  37.  
  38.         if (Input.GetKey(KeyCode.D))
  39.         {
  40.             // Make stuff work here.
  41.             transform.Translate(speed * Time.deltaTime, 0f,0f); //x,y,z
  42.            
  43.         }
  44.  
  45.         //8 Directional Movement Fix
  46.        
  47.         if ((Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D)) ||
  48.             (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.A)) ||
  49.             (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.A)) ||
  50.             (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.D)))
  51.         {
  52.             speed = 2.5f;
  53.         }
  54.         else
  55.         {
  56.             speed = 5f;
  57.         }
  58.  
  59.     }
  60. }
RAW Paste Data