Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Trying to do grid movement like First Person Dungeon Crawlers

Discussion in 'Scripting' started by piniacat, Feb 2, 2016.

  1. piniacat

    piniacat

    Joined:
    Apr 2, 2014
    Posts:
    2
    Hello there! I'm trying to replicate the movement from games like Etrian Odyssey (first person dungeon crawlers).

    What i want to do is to make the character move like it was in a grid, I mean... only advancing a fixed amount every time I press the Up arrow. I know how to do this in other languages in a 2D environtment, but I'm a little lost here. This is the code I'm using for the character right now.

    Code (CSharp):
    1. public class Movement : MonoBehaviour {
    2.  
    3.     public float moveSpeed = 10f;
    4.     public float turnSpeed = 90f;
    5.     public bool movement = false;
    6.  
    7.     void Update()
    8.     {
    9.         if (Input.GetKey(KeyCode.UpArrow))
    10.         {
    11.             transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
    12.         }
    13.         else if (Input.GetKey(KeyCode.DownArrow))
    14.         {
    15.             transform.Translate(-Vector3.forward * moveSpeed * Time.deltaTime);
    16.         }
    17.         else if (Input.GetKey(KeyCode.LeftArrow))
    18.         {
    19.             transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);
    20.         }
    21.         else if (Input.GetKey(KeyCode.RightArrow))
    22.         {
    23.             transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
    24.         }
    25.     }
    26.  
    27.     // Use this for initialization
    28.     void Start () {
    29.    
    30.     }
    31.    
    32.  
    33. }
    How it would work that? Where it isn't anything like doing a for and advancing 1pixel to front 16 times, I think. Also, I wanted to move the camera angle exactly 90º every time I press left or right. Advice on that? Thanks in advance (and sorry for the messy explanation).
     
  2. Craig8726

    Craig8726

    Joined:
    Jul 5, 2013
    Posts:
    79
    You could use a coroutine to execute when you input a keyDown and end when the transform.position has reached a desired increment away from the original position.
     
  3. Craig8726

    Craig8726

    Joined:
    Jul 5, 2013
    Posts:
    79
    Heres something if you want ot use translate. Its rough script that hasnt been checked and ive only filled in something for forward but you can use it as a template.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Movement : MonoBehaviour
    5. {
    6.  
    7.     public float moveSpeed = 10f;
    8.     public float turnSpeed = 90f;
    9.     public float moveDist = 5f;
    10.     public bool movement = false;
    11.     bool isMoving = false;
    12.     Vector3 currentPos;
    13.     Vector3 desiredPos;
    14.     public enum Direction
    15.     {
    16.         stationary,
    17.         forward,
    18.         back,
    19.         left,
    20.         right
    21.     }
    22.     public Direction currentDirection;
    23.     void Update ()
    24.     {
    25.         if (isMoving == false) {
    26.             if (Input.GetKeyDown (KeyCode.UpArrow)) {
    27.                 currentPos = transform.position;
    28.                 desiredPos = transform.position;
    29.                 desiredPos.z += moveDist;
    30.                 StartCoroutine ("movingForward");
    31.             } else if (Input.GetKeyDown (KeyCode.DownArrow)) {
    32.              
    33.             } else if (Input.GetKeyDown (KeyCode.LeftArrow)) {
    34.              
    35.             } else if (Input.GetKeyDown (KeyCode.RightArrow)) {
    36.              
    37.             }
    38.         }
    39.         switch (currentDirection) {
    40.         case Direction.stationary:
    41.             break;
    42.         case Direction.forward:
    43.             transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
    44.             break;
    45.         case Direction.back:
    46.             transform.Translate (-Vector3.forward * moveSpeed * Time.deltaTime);
    47.             break;
    48.         case Direction.left:
    49.             transform.Rotate (Vector3.up, -turnSpeed * Time.deltaTime);
    50.             break;
    51.         case Direction.right:
    52.             transform.Rotate (Vector3.up, turnSpeed * Time.deltaTime);
    53.             break;
    54.         }
    55.          
    56.  
    57.     }
    58.  
    59.     IEnumerator movingForward()
    60.     {
    61. isMoving = true;        
    62. currentDirection = Direction.forward;
    63.         while (currentPos.z < desiredPos.z) {
    64.             yield return null;
    65.         }
    66.         currentDirection = Direction.stationary;
    67. isMoving = false;
    68.     }
    69.  
    70.     // Use this for initialization
    71.     void Start ()
    72.     {
    73.  
    74.     }
    you could use lerps instead of translate to make it much simpler but you will have less control over the movement.
     
  4. piniacat

    piniacat

    Joined:
    Apr 2, 2014
    Posts:
    2
    I've tried with your code, but I can't adapt it very well. I don't understand much the use of the "deltaTime" instead of moving X pixels in a 2D game.

    Also, I've found a perfect example of what I'm trying to do in this video.



    Any directions on how to achieve something similar? (with the movement I mean, the other RPG things are not needed).

    Thanks!