Instantiated prefabs have no gravity AND weired script-behaviour
TLDR:
problem: If I instantiate prefabs into a variable (array), the resulting objects have no gravity and won't be affected by force (when throwing them they just stop the moment I release them). If I Instantiate them without a reference on them they work fine. why?
problem: Asking for a specific Tag in an if-clause won't work. Why?
(understanding) problem: OnTriggerEnter is executed when 2 objects collide and none of their colliders is marked as Trigger. Why?
I am a teacher and creating a little game for learning maths. It's about geometric solids. I use VRTK 4 Tilia for a System of Interactables and SnapZones. The current target for me is to get working, that I can spawn as many geometric solids (cylinder, wedge and cube) by dragging them out of a special SnapZone at the right side and to be able to stack those in a field of SnapZones, the "game field" of these zones. The Colliders of the SnapZones are Triggers, the colliders of the Interactables aren't.
2nd problem I have with ALL interactables (wedges, cubes and cylinders): I want those to snap in in the orientation, in which I drop them into the snapzone. Because of that, I wrote the following script named CopyRotationToCollidingObject:
private void OnTriggerEnter(Collider other)
{
//if (!other.CompareTag("Interactable") && !other.gameObject.CompareTag("Interactable"))
if(other.transform.parent.CompareTag("SnapZone"))
{
Quaternion rot = new Quaternion();
rot.w = this.gameObject.transform.rotation.w;
rot.x = this.gameObject.transform.rotation.x;
rot.y = this.gameObject.transform.rotation.y;
rot.z = this.gameObject.transform.rotation.z;
Vector3 angles = rot.eulerAngles;
angles.x = RoundMe(angles.x);
angles.y = RoundMe(angles.y);
angles.z = RoundMe(angles.z);
rot = Quaternion.Euler(angles);
other.gameObject.transform.parent.gameObject.transform.rotation = rot;
}
This generally works, when touching a SnapZone its orientation is changed, so that the snapped Interactable also is oriented in the right way. The problem with this is, that if I use the if-clause this way, nothing happens at all. If I use it the way that is commented out, the Interactables also switch the orientation of each other if they hit. The weired thing ist that if I Debug.Log exactly those tags before the if-clause, they say "Interactable" and "SnapZone", which is how I tagged the prefabs they come from. What am I doing wrong here? For my understanding this shouldn't be triggered at all when Interactables collide anywaay, cause none of their colliders are triggers (3rd problem). As a workaround with the following code I tried to delete the script from interactables when they are snapped to avoid the behaviour at least then, but it did NOTHING but actiating/deactivation the object toBeActivated, activate() is always called when an Interactable is snapped into a snapzone, deactivate() when an Interactable is unsnapped.
public void activate()
{
toBeActivated.SetActive(true);
Destroy(configurator.SnappedInteractable.GetComponent<CopyRotationToCollidingObject>());
}
public void deactivate()
{
configurator.SnappedInteractable.AddComponent<CopyRotationToCollidingObject>();
toBeActivated.SetActive(false);
}
1st problem: The prefabs I have in the beginning in the scene work perfectly (despite from problem #1). But when I instantiate new ones with the following class, I get weired problems: I can drag them out of the special spawn SnapZone, and immediately a new one is spawned that is snapped in that special spawn SnapZone. But then the one I just dragged out has no gravity!
public class PrefabGenerator : MonoBehaviour
{
public int limit=200;
[Tooltip("A Transform, which position (world space) is copied to the new instance of the prefab.")]
public Transform optionalSpawn;
public GameObject myPrefab;
[HideInInspector]
public static GameObject[] instances;
[HideInInspector]
public static int instanceCount = 0;
private void Start()
{
instances = new GameObject[limit];
}
public void generatePrefab()
{
if (optionalSpawn != null)
{
x = optionalSpawn.position.x;
y = optionalSpawn.position.y;
z = optionalSpawn.position.z;
}
if (instances[instanceCount] == null)
{
instances[instanceCount] = Instantiate(myPrefab, new Vector3(x, y, z), Quaternion.identity);
instances[instanceCount].transform.localScale = new Vector3(0.08f, 0.08f, 0.08f);
instanceCount++;
}
}
The prefab generator script is attached to the corresponding snap zone (there is one for each kind of geometric solid), so that the Start-routine is only executed at the start and not with each spawn. Why don't I have gravity on all during runtime created objects? Somehow this happens when I assign the Instantiation to a variable, but I need to do this to relocate them and let them snap into the SnapZone at spawn!
I hope you can help me! Julian
do your prefabs even have any component that exposes them to gravity? Like, just because it's a prefab it does not automatically interact with the physics engine. You will need to assign a Rigidbody or create your own gravity simulation.
Yes, they do have a Rigidbody. The Interactables themselfs have a Rigidbody, their child is a $$anonymous$$eshContainer and thats child is the cube or wedge or Cylinder (Components of those are $$anonymous$$esh Filter, $$anonymous$$esh Renderer, Box/$$anonymous$$esh Collider and a script to generate the mesh itself)
Your answer
Follow this Question
Related Questions
How do I stop multiple spawns? 0 Answers
Camera being affected by sphere collider triggers 0 Answers
Trigger a box collider based on force of an object 1 Answer
Falling Through Floor after a certain number of Triggers... 0 Answers
How do I make trigger destroy an object,with the collision from the player 1 Answer