Размер видео: 1280 X 720853 X 480640 X 360
Показать панель управления
Автовоспроизведение
Автоповтор
Thanks for the amazing tutorial man!
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); } }}
How to fix that it goes through objects when player is standing on it?
make sure those objects have rigidbodies and colliders
where can i copy these scripts
Thanks for the amazing tutorial man!
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);
}
}
}
How to fix that it goes through objects when player is standing on it?
make sure those objects have rigidbodies and colliders
where can i copy these scripts