Guest User

Tercera Parte: Movimiento Básico

a guest
Dec 27th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Player : MonoBehaviour {
  6.  
  7. public float maxSpeed = 5f;
  8. public float speed = 2f;
  9.  
  10. private Rigidbody2D rb2d;
  11.  
  12. // Use this for initialization
  13. void Start () {
  14. rb2d = GetComponent<Rigidbody2D>();
  15. }
  16.  
  17. // Update is called once per frame
  18. void Update () {
  19.  
  20. }
  21.  
  22. void FixedUpdate() {
  23. float h = Input.GetAxis("Horizontal"); // Aqui lo que conseguimos es que se guarde un valor negativo hasta -1 cuando vayamos a la izquierda, y un valor positivo hasta 1 cuando vayamos a la derecha.
  24. rb2d.AddForce(Vector2.right * speed * h);
  25.  
  26. // PARTE B
  27. if (rb2d.velocity.x > maxSpeed) { // Esto significa que nos estamos moviendo a la derecha a una velocidad mayor a la que deberiamos.
  28. rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);
  29. }
  30.  
  31. if (rb2d.velocity.x < -maxSpeed) { // Esto significa que nos estamos moviendo a la izquierda a una velocidad mayor a la que deberiamos.
  32. rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);
  33. }
  34.  
  35. Debug.Log (rb2d.velocity.x);
  36.  
  37.  
  38.  
  39. }
  40.  
  41. }
Advertisement
Add Comment
Please, Sign In to add comment