Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by JulesPeace · Feb 01, 2021 at 06:15 PM · triggersspawning problemstagging

Instantiated prefabs have no gravity AND weired script-behaviour

TLDR:

  1. 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?

  2. problem: Asking for a specific Tag in an if-clause won't work. Why?

  3. (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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AbandonedCrypt · Feb 02, 2021 at 10:39 AM 0
Share

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.

avatar image JulesPeace AbandonedCrypt · Feb 02, 2021 at 05:01 PM 0
Share

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)

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

155 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges