Advertisement
Guest User

Movimiento

a guest
Aug 19th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.37 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Movimiento : MonoBehaviour
  6. {
  7.     public float velocidadDeMovimiento = 5;
  8.     public float fuerzaDeSalto = 50;
  9.     public float aceleracion = 2;
  10.     private Rigidbody2D rb;
  11.     public Detector detectorPiso;
  12.     public bool enElPiso;
  13.    
  14.     private void Awake()
  15.     {
  16.         rb = GetComponent<Rigidbody2D>();
  17.         detectorPiso.onReady += EnElPiso;        
  18.     }
  19.     private void Update()
  20.     {
  21.         float x = Input.GetAxis("Horizontal");
  22.         float y = Input.GetAxis("Vertical");
  23.         Vector2 direction = new Vector2(x, y);
  24.         Walk(direction);
  25.         if(Input.GetButtonDown("Jump") && enElPiso)
  26.         {
  27.             Jump(Vector2.up);
  28.         }        
  29.     }
  30.     private void Walk (Vector2 dir)
  31.     {
  32.         if(enElPiso)
  33.         {
  34.             rb.velocity = Vector2.Lerp(rb.velocity,
  35.                 new Vector2(dir.x*velocidadDeMovimiento, dir.y),
  36.                 aceleracion * Time.deltaTime);
  37.         }
  38.         else
  39.         {
  40.             rb.velocity = new Vector2(dir.x * velocidadDeMovimiento, rb.velocity.y);
  41.         }
  42.     }
  43.     private void Jump(Vector2 dir)
  44.     {
  45.         rb.velocity = new Vector2(rb.velocity.x, 0);
  46.         rb.velocity += dir * fuerzaDeSalto;
  47.     }
  48.     void EnElPiso(bool a)
  49.     {
  50.         enElPiso = a;
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement