Как запретить движение kinematic 2d обьекта в одну сторону при достижении верха карты
Необходимо запретить движение наверх при достижении конца карты (около 1.1 по y) , оставив остальные направления. Игра 2D. Скрипт движения:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveInput : MonoBehaviour
{
public float speed = 3F; // скорость
public ControlType controlType; //андроид или пк
private Rigidbody2D rb;
public Vector2 moveInput;
public Vector2 moveVelocity;
public Joystick joystick;
public enum ControlType{PC, Android} //андроид или пк
private Animator animator;
public Animator anim;
private SpriteRenderer sprite;
private bool FacingRight; //поворот
public Transform position;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
sprite = GetComponentInChildren<SpriteRenderer>();
}
void Start()
{
if(controlType == ControlType.Android)
{
joystick.gameObject.SetActive(true);
}
if(controlType == ControlType.PC)
{
joystick.gameObject.SetActive(false);
}
}
// Update is called once per frame
void Update()
{
moveVelocity = moveInput.normalized * speed;
if(controlType == ControlType.PC)
{
moveInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
}
else if(controlType == ControlType.Android)
{
moveInput = new Vector2(joystick.Horizontal, joystick.Vertical);
}
if(moveInput.y == 0)
{
anim.SetBool("IsRunning", false);
}
else if(moveInput.y != 0)
{
anim.SetBool("IsRunning", true);
}
if(!FacingRight && moveInput.x < 0)
{
Flip(); //разворот
}
if(FacingRight && moveInput.x > 0)
{
Flip(); //разворот
}
}
void FixedUpdate()
{
rb.MovePosition(rb.position + moveVelocity * Time.fixedDeltaTime);
if(moveInput.x == 0)
{
anim.SetBool("IsRunning2", false);
}
else if(moveInput.x != 0)
{
anim.SetBool("IsRunning2", true);
}
}
private void Flip() //разворот
{
FacingRight = !FacingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
}