Unity - platformer pressure plate mechanic

Поделиться
HTML-код
  • Опубликовано: 24 дек 2024

Комментарии • 5

  • @Frvgii2368
    @Frvgii2368 Год назад

    Thanks for the amazing tutorial man!

  • @bbhppng
    @bbhppng 5 месяцев назад

    Here's more optimized code I guess. I removed OnCollisionStay (in my case it's trigger, you can change it if necessary).
    using UnityEngine;
    public class PressurePlate : MonoBehaviour
    {
    private Vector3 _originalPos;
    private bool _isPressed;
    private SpriteRenderer _spriteRenderer;
    [SerializeField] private float _maxDownDistance = 0.1f;
    void Start()
    {
    _originalPos = transform.position;
    _spriteRenderer = GetComponent();
    }
    private void OnTriggerEnter2D(Collider2D other)
    {
    if (other.CompareTag("Player"))
    {
    _isPressed = true;
    _spriteRenderer.color = Color.green;
    }
    }
    private void OnTriggerExit2D(Collider2D other)
    {
    if (other.CompareTag("Player"))
    {
    _isPressed = false;
    _spriteRenderer.color = Color.red;
    }
    }
    void FixedUpdate()
    {
    if (_isPressed && transform.position.y > _originalPos.y - _maxDownDistance)
    {
    transform.Translate(0f, -0.01f, 0f);
    }
    else if (!_isPressed && transform.position.y < _originalPos.y)
    {
    transform.Translate(0f, 0.01f, 0f);
    }
    }
    }

  • @Geo-di7yf
    @Geo-di7yf Год назад

    How to fix that it goes through objects when player is standing on it?

    • @Zhancat
      @Zhancat  Год назад

      make sure those objects have rigidbodies and colliders

  • @sacrificesnobi.3935
    @sacrificesnobi.3935 8 месяцев назад

    where can i copy these scripts