Platforms dont detect my camera collider

I have my platform scripts where I want to detect collision with my camera
Camera has colliderer + istrigger = on
Platform has non kinematic body + collider and rigidbody.

I try to get reference to the other script but even the debug.log doesn’t get triggered.

The spawner object got the spawn script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Platform : MonoBehaviour
{
    private Spawn spawn;
    public GameObject spawner;

    // Start is called before the first frame update
    void Start()
    {
        spawn = spawner.GetComponent<Spawn>();
    }

    // Update is called once per frame
    void Update()
    {
        transform.Translate(-Vector3.forward * Time.deltaTime);
        if(spawn == null) {
            Debug.Log("Spawn is null: " + spawn);
        } else if(spawn.objsSpawned == null) {
            Debug.Log("objSpawn is null: " + spawn.objsSpawned);
        }   
    }

    private void OnCollisionEnter(Collision other) {
        // If onCollison cam enter
        if (other.gameObject.CompareTag("MainCamera")) {
           spawn.AddPlatform();
        }       
    }

    private void OnCollisionExit(Collision other) {
        // If onCollsion cam exist
        if (other.gameObject.CompareTag("MainCamera")) {
            spawn.RemovePlatform();
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Spawn : MonoBehaviour
{
    public List<GameObject> objsSpawned;
    public float objectDistance;
    [SerializeField]
    private float maxDistance;

    void Start()
    {
        SpawnPlatform();      
    }

    void Update()
    {

    }
   
    // Spawn objects
    public void SpawnPlatform()
    {
        Vector3 offset = transform.position;
        for (int i = 0; i < objsSpawned.Count; i++)
            {
                offset += new Vector3(0f, 0f, objectDistance);
                var obj = Instantiate(objsSpawned[i], offset, Quaternion.identity);
            }
    }

    public void AddPlatform()
    {
        // If onCollison cam enter
        for (int i = 0; i < objsSpawned.Count; i++) {
                objsSpawned.Insert(objsSpawned.Count - 1, objsSpawned[i]);
            }
        Debug.Log("Enter");
    }      
   
    public void RemovePlatform()
    {
        objsSpawned.RemoveAt(0);
        Debug.Log("Exit");
    }
}

When you’re doing collisions with trigger colliders the relevant Unity messages are “OnTriggerEnter” and “OnTriggerExit”. Make sure you use a “Collider” parameter as well instead of “Collision”.

I got the collider fixed not errors, but I want to shift my element of position 0 to behind, but nothing happen, i guess something with transform.position?

    // Add an platform behind the list
    public void AddPlatform()
    {
        Debug.Log("Insert! " + objsSpawned.Count);
        objsSpawned.Insert(objsSpawned.Count - 1, objsSpawned[0]);
    }

If you manipulate the transform directly, you are completely bypassing the physics system.

From the physics system standpoint, this is a pure “teleport” and gives it no chance to fire collisions and triggers.

Instead, use the .MovePosition() and .MoveRotation() methods on the Rigidbody to move things if you are interested in the physics events.

2 Likes