Advertisement
PluzVS

Unity 2D RPG PlayerMovement

Nov 18th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.23 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerMovement : MonoBehaviour {
  6.  
  7.     #region PlayerStats
  8.     // Velocidade do player
  9.     public float speed = 2.5f;
  10.     #endregion
  11.  
  12.     #region UnityEvents
  13.     void Start () {
  14.     }
  15.    
  16.     void Update () {
  17.             Moviment();
  18.     }
  19.     #endregion
  20.  
  21.     #region Methods
  22.     /// <summary>
  23.     /// Movimentação do Player
  24.     /// </summary>
  25.     void Moviment()
  26.     {
  27.         // Valores do eixo virtual (Sempre entre -1 e 1)
  28.         float horizontal = Input.GetAxisRaw("Horizontal"); // Botões horizontais "A"e "D"
  29.         float vertical = Input.GetAxisRaw("Vertical"); // Botões verticais "W" e "S"
  30.  
  31.         // deltaTime = O tempo em segundos que demorou para completar o último frame
  32.         // Translate = Move o transform na direção e distancia desejada.
  33.         if (horizontal != 0)
  34.         {
  35.             float x;
  36.             x = horizontal * Time.deltaTime * speed;
  37.             transform.Translate(x, 0, 0);
  38.            
  39.         }
  40.  
  41.         if (vertical != 0)
  42.         {
  43.             float y;
  44.             y = vertical * Time.deltaTime * speed;
  45.             transform.Translate(0, y, 0);
  46.         }
  47.     }
  48.     #endregion
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement