Hello, I have a player which has a box collider not set to trigger, and it has a rigidbody. I also have 4 empty objects which have colliders which is set to trigger, with no rigidbody.
When the player collides with any of these 4 colliders, i want to change the code that is in a different script, because of this, i want the trigger script on the player, but is that possible? Does the trigger script need to be on the colliders that have trigger enabled?
The 4 colliders have different names, so it would be beneficial for this to be on the player object, but when i tried it is just now detecting the OntriggerEnter . I have read around and people seem to suggest putting it on the objects that have trigger enabled, but nothing saying it has to be
Your script can be on either object involved in the collision. You can read any information you want about the object you collider with from the parameter you receive in your OnTriggerEnter function.
ok thank you, that is good to know. Not wasting my time then trying to figure out why it is not working
S
Feel free to post your code here if you feel like you’ve hit a wall.
Hi, I still cant seem to figure this out. At first I thought it could have something to do with my player movement code, but I downloaded the 3rd person controller script from the store and its still not working, so I have no idea. By chance does anyone see what my issue is here. This script is attached to the player.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cameraColliders : MonoBehaviour
{
public GameObject camCollider1;
public GameObject camCollider2;
public GameObject camCollider3;
public GameObject camCollider4;
public cameraController camControllerScript;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void OntriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("camCollider1"))
{
Debug.Log("Camera 1 active");
camControllerScript.cameraArray[0].gameObject.SetActive(true);
camControllerScript.cameraArray[1].gameObject.SetActive(false);
camControllerScript.cameraArray[2].gameObject.SetActive(false);
camControllerScript.cameraArray[3].gameObject.SetActive(false);
}
else if (other.gameObject.CompareTag("camCollider2"))
{
Debug.Log("Camera 2 active");
camControllerScript.cameraArray[0].gameObject.SetActive(false);
camControllerScript.cameraArray[1].gameObject.SetActive(true);
camControllerScript.cameraArray[2].gameObject.SetActive(false);
camControllerScript.cameraArray[3].gameObject.SetActive(false);
}
else if (other.gameObject.CompareTag("camCollider3"))
{
Debug.Log("Camera 3 active");
camControllerScript.cameraArray[0].gameObject.SetActive(false);
camControllerScript.cameraArray[1].gameObject.SetActive(false);
camControllerScript.cameraArray[2].gameObject.SetActive(true);
camControllerScript.cameraArray[3].gameObject.SetActive(false);
}
else if (other.gameObject.CompareTag("camCollider4"))
{
Debug.Log("Camera 4 active");
camControllerScript.cameraArray[0].gameObject.SetActive(false);
camControllerScript.cameraArray[1].gameObject.SetActive(false);
camControllerScript.cameraArray[2].gameObject.SetActive(false);
camControllerScript.cameraArray[3].gameObject.SetActive(true);
}
}
}
All the cameraControllerScript has is an array of cameras.
You’ve misspelled OnTriggerEnter as OntriggerEnter.
Thank you very much, that was driving me insane. I dont believe i spent hours trying to solve it, and it was a stupid spelling mistake
Properly set up, Visual Studio should be suggesting this stuff for you when you first begin to type “void OnTri…”
This may help you with intellisense and possibly other Visual Studio integration problems:
https://discussions.unity.com/t/778503
Also, try update the VSCode package inside of Unity: Window → Package Manager → Search for Visual Studio Code Editor → Press the Update button
Also, this: https://discussions.unity.com/t/805330/7
1 Like