Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using UnityEngine;
- /*
- * A simple dungeon crawler player movement script
- * Written by zooperdan
- *
- * 1) Create a layer named "Blocking" (or any other name you choose)
- * 2) Add this script to the Player gameobject.
- * 3) Set "Layer Mask" in inspector to the "Blocking" layer.
- * 4) Add Wall objects (Quads or Cubes) with colliders and place them on grid positions relative to PlayerMovement.stepSize
- * 5) Add all walls to the "Blocking" layer.
- *
- * Note: This script use iTween by Bob Berkebile of pixelplacement.com and it is free to use.
- * http://www.pixelplacement.com/itween/index.php
- * https://assetstore.unity.com/packages/tools/animation/itween-84
- *
- */
- public class PlayerMovement : MonoBehaviour
- {
- public LayerMask layerMask;
- public bool continuedWalk = true;
- public float walkSpeed = 1.0f;
- public float turnSpeed = 1.0f;
- public float stepSize = 3.0f;
- public iTween.EaseType easeType;
- public KeyCode keyMoveForward = KeyCode.UpArrow;
- public KeyCode keyMoveBackward = KeyCode.DownArrow;
- public KeyCode keyStrafeLeft = KeyCode.Delete;
- public KeyCode keyStrafeRight = KeyCode.PageDown;
- public KeyCode keyTurnLeft = KeyCode.LeftArrow;
- public KeyCode keyTurnRight = KeyCode.RightArrow;
- private bool _isMoving = false;
- private bool _isTurning = false;
- void Update()
- {
- if (!_isMoving && !_isTurning)
- {
- if (KeyDown(keyMoveForward))
- {
- Move(transform.forward * stepSize);
- }
- if (KeyDown(keyMoveBackward))
- {
- Move(-transform.forward * stepSize);
- }
- if (KeyDown(keyStrafeLeft))
- {
- Move(-transform.right * stepSize);
- }
- if (KeyDown(keyStrafeRight))
- {
- Move(transform.right * stepSize);
- }
- }
- if (!_isTurning)
- {
- if (Input.GetKeyDown(keyTurnLeft))
- {
- Turn(-90f);
- }
- if (Input.GetKeyDown(keyTurnRight))
- {
- Turn(90f);
- }
- }
- }
- private bool KeyDown(KeyCode key)
- {
- return continuedWalk && !_isMoving ? Input.GetKey(key) : Input.GetKeyDown(key);
- }
- private bool IsBlocked(Vector3 direction)
- {
- if (Physics.Raycast(
- transform.position + new Vector3(0f, 0.5f, 0f),
- direction + new Vector3(0f, 0.5f, 0f),
- out RaycastHit hit,
- 2.99f, layerMask
- ))
- {
- /*
- * insert conditional checks here for colliding with
- * various blocking elements like doors, triggers etc.
- */
- return true;
- }
- else
- {
- return false;
- }
- }
- public void OnMoved()
- {
- _isMoving = false;
- transform.position = new Vector3(
- Mathf.RoundToInt(transform.position.x),
- 0.0f,
- Mathf.RoundToInt(transform.position.z)
- );
- }
- public void OnTurned()
- {
- _isTurning = false;
- var vec = transform.eulerAngles;
- vec.x = Mathf.Round(vec.x / 90) * 90;
- vec.y = Mathf.Round(vec.y / 90) * 90;
- vec.z = Mathf.Round(vec.z / 90) * 90;
- transform.eulerAngles = vec;
- }
- private void Move(Vector3 direction)
- {
- if (!IsBlocked(direction) && !_isMoving)
- {
- _isMoving = true;
- Hashtable ht = new Hashtable
- {
- { "amount", direction },
- { "time", walkSpeed },
- { "space", Space.World },
- { "oncomplete", "OnMoved" },
- { "easeType", easeType },
- { "looptype", iTween.LoopType.none }
- };
- iTween.MoveBy(this.gameObject, ht);
- }
- }
- private void Turn(float angle)
- {
- Vector3 rotationAmount = new Vector3(0, angle / 360, 0);
- if (!_isTurning)
- {
- _isTurning = true;
- Hashtable ht = new Hashtable
- {
- { "amount", rotationAmount },
- { "time", turnSpeed },
- { "oncomplete", "OnTurned" },
- { "easeType", easeType },
- { "looptype", iTween.LoopType.none }
- };
- iTween.RotateBy(this.gameObject, ht);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment