Hello everyone, excuse me for submitting this very simple problem but I’m a bit rusty with scripting and I’m not a professional programmer, I’m simply trying to make a sliding door, but it seems that the two box colliders (of the door and the player) don’t collide with each other, it doesn’t even start the debug I wrote under “On trigger enter” so they’re not really colliding, where am I going wrong?
I ensure that I have set the player object with the “Player” tag, and that both the door and player collider boxes are on and are set to “Is trigger”
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SlidingDoor : MonoBehaviour
{
public float slidingSpeed = 2f; // Velocità di scorrimento della porta
public float openPosition = -1.5f; // Posizione della porta quando aperta
public bool isOpen = false; // Flag per indicare se la porta è aperta o chiusa
public string playerTag = “Player”; // Tag dell’oggetto giocatore
private Vector3 closedPosition; // Posizione della porta quando chiusa
private Vector3 targetPosition; // Posizione target della porta
// Start is called before the first frame update
void Start()
{
closedPosition = transform.position; // Salva la posizione iniziale della porta
targetPosition = closedPosition; // Inizializza la posizione target alla posizione iniziale della porta
}
private void OnCollisionEnter(Collision collision)
{
Debug.Log(“OnCollisionEnter called”);
if (collision.gameObject.CompareTag(playerTag))
{
Debug.Log(“Player collided with the door”);
isOpen = true; // La porta è aperta
targetPosition = new Vector3(openPosition, transform.position.y, transform.position.z); // Imposta la posizione target della porta
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject.CompareTag(playerTag))
{
isOpen = false; // La porta è chiusa
targetPosition = closedPosition; // Imposta la posizione target della porta
}
}
private void FixedUpdate()
{
// Muove la porta verso la posizione target con una traslazione morbida
transform.position = Vector3.Lerp(transform.position, targetPosition, slidingSpeed * Time.fixedDeltaTime);
}
// Update is called once per frame
void Update()
{
}
}