zooperdan

A simple dungeon crawler player movement script

Apr 19th, 2021 (edited)
2,172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.63 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4.  
  5. /*  
  6.  *  A simple dungeon crawler player movement script
  7.  *  Written by zooperdan
  8.  *  
  9.  *  1) Create a layer named "Blocking" (or any other name you choose)
  10.  *  2) Add this script to the Player gameobject.
  11.  *  3) Set "Layer Mask" in inspector to the "Blocking" layer.
  12.  *  4) Add Wall objects (Quads or Cubes) with colliders and place them on grid positions relative to PlayerMovement.stepSize
  13.  *  5) Add all walls to the "Blocking" layer.
  14.  *  
  15.  *  Note: This script use iTween by Bob Berkebile of pixelplacement.com and it is free to use.
  16.  *  http://www.pixelplacement.com/itween/index.php
  17.  *  https://assetstore.unity.com/packages/tools/animation/itween-84
  18.  *  
  19.  */
  20.  
  21. public class PlayerMovement : MonoBehaviour
  22. {
  23.  
  24.     public LayerMask layerMask;
  25.  
  26.     public bool continuedWalk = true;
  27.  
  28.     public float walkSpeed = 1.0f;
  29.     public float turnSpeed = 1.0f;
  30.     public float stepSize = 3.0f;
  31.  
  32.     public iTween.EaseType easeType;
  33.  
  34.     public KeyCode keyMoveForward = KeyCode.UpArrow;
  35.     public KeyCode keyMoveBackward = KeyCode.DownArrow;
  36.     public KeyCode keyStrafeLeft = KeyCode.Delete;
  37.     public KeyCode keyStrafeRight = KeyCode.PageDown;
  38.     public KeyCode keyTurnLeft = KeyCode.LeftArrow;
  39.     public KeyCode keyTurnRight = KeyCode.RightArrow;
  40.  
  41.     private bool _isMoving = false;
  42.     private bool _isTurning = false;
  43.  
  44.     void Update()
  45.     {
  46.  
  47.         if (!_isMoving && !_isTurning)
  48.         {
  49.  
  50.             if (KeyDown(keyMoveForward))
  51.             {
  52.                 Move(transform.forward * stepSize);
  53.             }
  54.  
  55.             if (KeyDown(keyMoveBackward))
  56.             {
  57.                 Move(-transform.forward * stepSize);
  58.             }
  59.  
  60.             if (KeyDown(keyStrafeLeft))
  61.             {
  62.                 Move(-transform.right * stepSize);
  63.             }
  64.  
  65.             if (KeyDown(keyStrafeRight))
  66.             {
  67.                 Move(transform.right * stepSize);
  68.             }
  69.  
  70.         }
  71.  
  72.         if (!_isTurning)
  73.         {
  74.  
  75.             if (Input.GetKeyDown(keyTurnLeft))
  76.             {
  77.                 Turn(-90f);
  78.             }
  79.  
  80.             if (Input.GetKeyDown(keyTurnRight))
  81.             {
  82.                 Turn(90f);
  83.             }
  84.  
  85.         }
  86.  
  87.     }
  88.  
  89.     private bool KeyDown(KeyCode key)
  90.     {
  91.  
  92.         return continuedWalk && !_isMoving ? Input.GetKey(key) : Input.GetKeyDown(key);
  93.  
  94.     }
  95.  
  96.     private bool IsBlocked(Vector3 direction)
  97.     {
  98.  
  99.         if (Physics.Raycast(
  100.             transform.position + new Vector3(0f, 0.5f, 0f),
  101.             direction + new Vector3(0f, 0.5f, 0f),
  102.             out RaycastHit hit,
  103.             2.99f, layerMask
  104.         ))
  105.         {
  106.             /*
  107.              * insert conditional checks here for colliding with
  108.              * various blocking elements like doors, triggers etc.
  109.              */
  110.             return true;
  111.         }
  112.         else
  113.         {
  114.             return false;
  115.         }
  116.  
  117.     }
  118.  
  119.     public void OnMoved()
  120.     {
  121.         _isMoving = false;
  122.         transform.position = new Vector3(
  123.             Mathf.RoundToInt(transform.position.x),
  124.             0.0f,
  125.             Mathf.RoundToInt(transform.position.z)
  126.         );
  127.     }
  128.  
  129.     public void OnTurned()
  130.     {
  131.         _isTurning = false;
  132.         var vec = transform.eulerAngles;
  133.         vec.x = Mathf.Round(vec.x / 90) * 90;
  134.         vec.y = Mathf.Round(vec.y / 90) * 90;
  135.         vec.z = Mathf.Round(vec.z / 90) * 90;
  136.         transform.eulerAngles = vec;
  137.     }
  138.  
  139.     private void Move(Vector3 direction)
  140.     {
  141.  
  142.         if (!IsBlocked(direction) && !_isMoving)
  143.         {
  144.             _isMoving = true;
  145.             Hashtable ht = new Hashtable
  146.                 {
  147.                     { "amount", direction },
  148.                     { "time", walkSpeed },
  149.                     { "space", Space.World },
  150.                     { "oncomplete", "OnMoved" },
  151.                     { "easeType", easeType },
  152.                     { "looptype", iTween.LoopType.none }
  153.                 };
  154.             iTween.MoveBy(this.gameObject, ht);
  155.         }
  156.  
  157.     }
  158.  
  159.     private void Turn(float angle)
  160.     {
  161.  
  162.         Vector3 rotationAmount = new Vector3(0, angle / 360, 0);
  163.  
  164.         if (!_isTurning)
  165.         {
  166.             _isTurning = true;
  167.             Hashtable ht = new Hashtable
  168.                 {
  169.                     { "amount", rotationAmount },
  170.                     { "time", turnSpeed },
  171.                     { "oncomplete", "OnTurned" },
  172.                     { "easeType", easeType },
  173.                     { "looptype", iTween.LoopType.none }
  174.                 };
  175.             iTween.RotateBy(this.gameObject, ht);
  176.         }
  177.  
  178.     }
  179.  
  180. }
Advertisement
Add Comment
Please, Sign In to add comment