Skip to content

Instantly share code, notes, and snippets.

@muddokon
Created August 12, 2020 00:51
Show Gist options
  • Save muddokon/b42f81c68129f9656419231ea8f42f72 to your computer and use it in GitHub Desktop.
Save muddokon/b42f81c68129f9656419231ea8f42f72 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LimitarVuelo : MonoBehaviour
{
private float xOff;
[Header("Separacion del sprite con el borde de la pantalla en X izquierda:")]
public float xOffsetLeft;
[Header("Separacion del sprite con el borde de la pantalla en X derecha:")]
public float xOffsetRight;
[Header("Separacion del sprite con el borde de la pantalla en Y abajo:")]
public float yOffsetInf;
[Header("Separacion del sprite con el borde de la pantalla en Y arriba:")]
public float yOffsetSup;
void Awake()
{
xOff = gameObject.GetComponent<SpriteRenderer>().size.x / 2;
playerRigidbody = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
//Descomponemos el vector posición con Clamp para cada componente x,y,z dentro de los límites
transform.position = new Vector3(Mathf.Clamp(transform.position.x, xOffsetLeft, xOffsetRight), Mathf.Clamp(transform.position.y, yOffsetInf, yOffsetSup), transform.position.z);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment