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

Tile Movements

Discussion in 'Scripting' started by Burton-kun, Aug 14, 2015.

  1. Burton-kun

    Burton-kun

    Joined:
    Aug 6, 2015
    Posts:
    50
    Hello everybody. I'm new to the community and I'm new to C#, i got a problem.
    I'm trying to write down a script able to make my player walks on a grid.
    I saw the gridMove on the Unify Wiki but it's not what I'm looking for, so I'm trying by myself.
    Here what I've been doing:

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerMovement : MonoBehaviour {
    6.     private float MoveSpeed = 1.2f;
    7.     private float GridSize = 18f;
    8.  
    9.     // Update is called once per frame
    10.     void Update () {
    11.  
    12.         //Player Rotation
    13.         if (Input.GetKeyDown (KeyCode.UpArrow)) {
    14.             transform.forward = new Vector3 (0,0,-1);
    15.         }
    16.         else if (Input.GetKeyDown (KeyCode.DownArrow)) {
    17.             transform.forward = new Vector3 (0,0,1);
    18.         }
    19.         else if (Input.GetKeyDown (KeyCode.RightArrow)) {
    20.             transform.forward = new Vector3 (-1,0,0);
    21.         }
    22.         else if (Input.GetKeyDown (KeyCode.LeftArrow)) {
    23.             transform.forward = new Vector3 (1,0,0);
    24.         }
    25.  
    26.         //Player translation
    27.         if (Input.GetKey (KeyCode.UpArrow)) {
    28.             transform.Translate (Vector3.back *GridSize, Space.Self);
    29.         }
    30.         else if (Input.GetKey (KeyCode.DownArrow)) {
    31.             transform.Translate (Vector3.back * GridSize, Space.Self);
    32.         }
    33.         else if (Input.GetKey (KeyCode.LeftArrow)) {
    34.             transform.Translate (Vector3.back * GridSize, Space.Self);
    35.         }
    36.  
    37.         else if (Input.GetKey (KeyCode.RightArrow)) {
    38.             transform.Translate (Vector3.back * GridSize, Space.Self);
    39.         }
    40.     }
    41.  
    The player moves perfectly on the grid wich size is 18, but it does with an elevate speed.
    I setted a MoveSpeed value, but i don't know how to use it in the script to regulate the player movements. As I said before, I'm new to C#, can someone Help me?
    The game I'm working on it's a Pokèmon FanGame with a 3D style, similiar to the last ones from Game Freak.
     
  2. Carvuh

    Carvuh

    Joined:
    Mar 25, 2013
    Posts:
    25
    What this script will do is basically move the player as if the player were moving on tiles only. Think of like a dungeon crawler or something similar to old style RPG's. In order to get your pokemon fan game going, you're going to have to do continuous movements but only only the X and Y axis's, but only 1 at a given time.

    You are also translate the character back multiple times in the same direction with different key strokes?

    Try something like:

    Code (csharp):
    1.  // We can remove GridSize because it's not really a "Grid" movement.
    2. // We use GetKey() instead of GetKeyDown() because we want to run the state WHILE the key is pressed.
    3. if (Input.GetKey(KeyCode.W)) {
    4.     transform.Translate(Vector3.forward * Time.deltaTime, Space.self);
    5. }
    6. // You can also use these for the other directions:
    7. // Vector3.back;
    8. // Vector3.right;
    9. // Vector3.left;
    10.  
     
  3. Burton-kun

    Burton-kun

    Joined:
    Aug 6, 2015
    Posts:
    50
    Yes, with different keys it translates always back but the trick is in the rotation.
    The player, before moving, face the direction so, once it's looking right, for example, i can't make it move right, but I need to set "back" (due to the mesh align) to make it move in that direction.
    I tried "time.deltatime" but he doesn't move tile by tile. It's like he moves pixel by pixel. I use an entire mesh as map made in Blender, and i build it with a grid made of block which size is 18. Like it was a fake tile mapping.
    As i said, the problem isn't the moving, but the speed. if i write:
    Code (CSharp):
    1. transform.Translate (Vector3.back * GridSize * MoveSpeed, Space.Self)
    It doesn't walk following the grid and if I just write:
    Code (CSharp):
    1. transform.Translate (Vector3.back *GridSize, Space.Self)
    It follows the grid, but make more than one step due to the speed.

    I used "getkeydown" only in "transform.forward" 'cause in a typical pokèmon game, by pressing the arrow once time, the player just face the direction but doesn't move, if you keep pressing instead, the player move. I'm trying to recreate this effect but i can't..