Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Movimiento : MonoBehaviour
- {
- public float velocidadDeMovimiento = 5;
- public float fuerzaDeSalto = 50;
- public float aceleracion = 2;
- private Rigidbody2D rb;
- public Detector detectorPiso;
- public bool enElPiso;
- private void Awake()
- {
- rb = GetComponent<Rigidbody2D>();
- detectorPiso.onReady += EnElPiso;
- }
- private void Update()
- {
- float x = Input.GetAxis("Horizontal");
- float y = Input.GetAxis("Vertical");
- Vector2 direction = new Vector2(x, y);
- Walk(direction);
- if(Input.GetButtonDown("Jump") && enElPiso)
- {
- Jump(Vector2.up);
- }
- }
- private void Walk (Vector2 dir)
- {
- if(enElPiso)
- {
- rb.velocity = Vector2.Lerp(rb.velocity,
- new Vector2(dir.x*velocidadDeMovimiento, dir.y),
- aceleracion * Time.deltaTime);
- }
- else
- {
- rb.velocity = new Vector2(dir.x * velocidadDeMovimiento, rb.velocity.y);
- }
- }
- private void Jump(Vector2 dir)
- {
- rb.velocity = new Vector2(rb.velocity.x, 0);
- rb.velocity += dir * fuerzaDeSalto;
- }
- void EnElPiso(bool a)
- {
- enElPiso = a;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement