Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Player : MonoBehaviour {
- public float maxSpeed = 5f;
- public float speed = 2f;
- private Rigidbody2D rb2d;
- // Use this for initialization
- void Start () {
- rb2d = GetComponent<Rigidbody2D>();
- }
- // Update is called once per frame
- void Update () {
- }
- void FixedUpdate() {
- 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.
- rb2d.AddForce(Vector2.right * speed * h);
- // PARTE B
- if (rb2d.velocity.x > maxSpeed) { // Esto significa que nos estamos moviendo a la derecha a una velocidad mayor a la que deberiamos.
- rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);
- }
- if (rb2d.velocity.x < -maxSpeed) { // Esto significa que nos estamos moviendo a la izquierda a una velocidad mayor a la que deberiamos.
- rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);
- }
- Debug.Log (rb2d.velocity.x);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment