- Home /
I can't figure it out.
I have done everything I can to create a SceneChanging script. There are no error, I set it as Trigger, I gave the player a Collider and it just doesn't work. I would love is somebody would try my game out and tell me what the error is. A link to it is here: http://www.mediafire.com/file/m76vm36vg967kne/FPS_openworld.1.unity.zip
PS! Important: The teleportation block is the Gold block on top of the hill. The player is a FPSController named "Player". To get up there either solve the puzzle, or place the Player up on the mountain. Thank you very much!
Scene without the actual assets is broken. You should upload the whole project.
Answer by Legend_Bacon · Mar 22, 2018 at 01:10 PM
Hello there,
Could you give us a little more information please? Like post your code, tell us what you are trying to do, describe which components are on your objects, etc... We can't help you if your question is that vague.
Other than that, you also need to upload the actual project (or a build) if you want us to test it. A scene file can't work without your scripts, assets, settings, etc...
UPDATE:
Site wouldn't let me post a new comment, so here it is:
I didn't make a prefab per say, but this will help you regardless. Just make a new script and call it "OnCollisionLoadScene". Then, you can place it on your cube.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
[RequireComponent(typeof(Collider))]
public class OnCollisionLoadScene : MonoBehaviour
{
[SerializeField] private string sceneToLoad = "";
//Check Collision against all tags in this list
[SerializeField] private List<string> list_tagsToWatchFor = new List<string>();
// Enable OnCollisionEnter
[SerializeField] private bool checkForCollision = false;
// Enable OnTriggerEnter
[SerializeField] private bool checkForTrigger = false;
//Enable DistanceCheck
[SerializeField] private bool checkForDistance = false;
[SerializeField] private float distanceToCheck = 0.0f; // Radius to check
[SerializeField] private float distanceCheckRefreshEvery = 0.0f; // Check for distance every X seconds
private void Start()
{
if (checkForDistance)
StartCoroutine(CheckForDistanceCoroutine());
}
private void OnCollisionEnter(Collision collision)
{
if (!checkForCollision)
return;
if (list_tagsToWatchFor.Contains(collision.gameObject.tag))
{
Debug.Log("COLLISION: LOAD SCENE");
SceneManager.LoadScene(sceneToLoad);
}
}
private void OnTriggerEnter(Collider collision)
{
if (!checkForTrigger)
return;
if (list_tagsToWatchFor.Contains(collision.gameObject.tag))
{
Debug.Log("TRIGGER: LOAD SCENE");
SceneManager.LoadScene(sceneToLoad);
}
}
private IEnumerator CheckForDistanceCoroutine()
{
while (checkForDistance)
{
List<GameObject> objectsToCheck = new List<GameObject>();
foreach (string item in list_tagsToWatchFor)
objectsToCheck.AddRange(GameObject.FindGameObjectsWithTag(item));
foreach (GameObject item in objectsToCheck)
{
if (Vector3.Distance(transform.position, item.transform.position) <= distanceToCheck)
{
Debug.Log("DISTANCE: LOAD SCENE");
SceneManager.LoadScene(sceneToLoad);
break;
}
}
yield return new WaitForSeconds(distanceCheckRefreshEvery);
}
yield return null;
}
//Purely for distance vizualisation in the scene
#if UNITY_EDITOR
private void OnDrawGizmos()
{
Gizmos.color = new Color(0.65f, 0.0f, 0.0f, 0.25f);
Gizmos.DrawSphere(transform.position, distanceToCheck);
}
#endif
}
• The collision works IF your player has a Collider NOT trigger and a rigidbody.
• The TRIGGER collision works IF your player has a Collider trigger and a rigidbody.
• The distance check works continuously as long as you have it enabled.
Don't forget to fill the fields you need in the inspector before using it!
Hope that helps!
Cheers,
~LegendBacon
Hello! The whole file compressed is still quite big (Around 700$$anonymous$$B). Do you know if there is any easier way to do this? Let me explain what I am trying to do. I am trying to get it so that when my FPSController, with a Rigidbody and Collider, (who is tagged "Player) when he touches a Golden Cube with a box collider with isTrigger checked, changes to the next scene. $$anonymous$$y script is:
using UnityEngine.Scene$$anonymous$$anagement;
using UnityEngine;
public class NewBehaviourScript : $$anonymous$$onoBehaviour
{
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Player")
{
Scene$$anonymous$$anager.LoadScene ("Pklvl2");
}
}
}
Sorry for being so vague.
EDIT: Also I am uploading my Project to files.fm under this link: https://files.fm/u/jq4ghxr3 . Once it is done I will reply again incase you still want to see the Project file! :)
Ah, I see. Thank you for uploading the script.
Unfortunately, a Trigger will not raise the OnCollisionEnter event when colliding with a Collider.
You have two solutions here:
Change your cube to NOT be a trigger. Then everything works as you have it.
Add a second Collider to your player, and mark it as Trigger. Then, change your method to:
void OnTriggerEnter(Collider collision) { if (collision.gameObject.tag == "Player") { Debug.Log("COLLISION"); } }
Hope that helps!
Cheers,
~LegendBacon
Hello there! Sorry about taking so long to respond. It still doesn't work using both methods. I know this may be a bit much to ask, but do you think you could create a prefab of a scene changing item. I would really appreciate it. You don't have to do it any time soon, but if you have some free time could you please try it? Thank you so much!