Advertisement
gomuani

Movimiento Horizontal

Jun 11th, 2025
855
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.61 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class MovimientoHorizontal : MonoBehaviour
  6. {
  7.     public float velocidad = 5f;
  8.     private Rigidbody2D rb;
  9.     private float movimiento;
  10.  
  11.     void Start()
  12.     {
  13.         rb = GetComponent<Rigidbody2D>();
  14.     }
  15.  
  16.     void Update()
  17.     {
  18.         // Movimiento horizontal con teclas A/D o flechas izquierda/derecha
  19.         movimiento = Input.GetAxisRaw("Horizontal");
  20.     }
  21.  
  22.     void FixedUpdate()
  23.     {
  24.         // Solo se mueve en el eje X
  25.         rb.velocity = new Vector2(movimiento * velocidad, rb.velocity.y);
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement