How best to modify colliders to affect pickup area for character? - c#

I am making a simple inventory/equipment system with a basic square character. One of the items you can use is a potion that will increase the pickup item area. I have a way to do this already, but I am wondering if there is a better way to do this?
WHAT I CURRENTLY HAVE
I have a collider on my character object, which deals with collisions with the walls and general world (all this handled in the Player script).
I then have an empty gameObject child of this object, which has a collider component the same size as the other collider (at start).
The child gameObject has a script (PickupAreaModifier script) with a OnTriggerEnter2D function that calls a method in the main character script to add the item.
If the item is the potion, then the Player script calls a method in the PickupAreaModifier script to change the size of that collider.
WHAT I WANT
This current logic works, but I would like to know if there is a more efficient way to handle this, especially considering the OnTriggerEnter2D function needing to be called on the child gameObject script. Is there any way I can handle this in only the Player script?
Thank you in advance, I hope that makes sense!

Related

Specify all GameObjects?

I was wondering if there was a way to specify all game objects. I am trying to calculate the distance between the player and the grounds/every game object but the player and what it's made up of. anyway, if anyone knows how to get the distance of the nearest gameobject to the player please let me know.
You can get all GameObjects in your Scene using GameObject.FindGameObjectsOfType<GameObject>() because this will get every GameObject that is a GameObject, meaning everyone.
If you also want to get inactive GameObjects, you can use GameObject.FindGameObjectsOfType<GameObject>(true)
However, I don't think you really want every GameObject. You probably either want everyone that has a collider or everyone that has a Renderer.
If you want to get all GameObjects with a Collider, you can use GameObject.FindGameObjectsOfType<Collider>() for 3D Colliders or GameObject.FindGameObjectsOfType<Collider2D>() for 2D Colliders
If you want to get all GameObjects wich are being rendered, you can use GameObject.FindGameObjectsOfType<Renderer>().
Of course, you can put true between the parentheses of all of these to get inactive ones too.
However
It's generally not a good idea to get so many objects because it's very bad for performance.

Is there a way to make clickable/interactable particles in Unity?

I want to create a bubble-pop mechanic where bubbles will fall from the top of the screen, and the player will be able to 'pop' them by clicking/tapping on them.
Basically this: https://www.youtube.com/watch?v=OH4dcpSk7n0
I started by creating a particle system. But is there a way to attach a script to the particle? Or should I go about it in a completely different way?
you should not use particles to achieve this, instead you should make a GameObject with rigidBody and collider attached to it, and spawn those GameObjects in the position you want.

Unity C# capsule collider and rigid body don't trigger using transform.position to move

I have 2 simple capsules. 1 is stationary and the other is moving to the same tile on the tilemap using transform.position.
Both capsules have capsule colliders and rigid bodies. I've attempted to remove the rigid body but from what I can tell, the OnCollisionEnter function requires a rigid body to work.
My script, attached to both of these, is a simple:
private void OnCollisionEnter(Collision other)
{
print("Collision Detected! " + other.gameObject.name);
}
I've used combinations of 'isKinematic', 'isTrigger', placing one component before the other then reversing, ensuring I have the script attached to both objects, making sure my capsule collider is sized to the object, and looked at other people's issues to try their steps to fix my issue.
Nothing seems to trigger the collision between the 2 capsules. It's got to be something small I'm missing.
I have a tag on both objects and have tried things like if(other.gameObject.tag == "myTag") {...} but that's not working for me, either.
Can anyone spot my mistake? Let me know if you need to see anything else - happy to provide images or whatever will help. Thanks in advance!
Above is the basic identical hierarchy of both capsules with components attached to the capsule mesh. Both have the parent empty objects they're stationed under. (Which themselves, contain scripts but no collider or rigid body)
Here is an infographic to show when a collision message will be detected by OnCollisionEnter between two objects. Both objects will need some sort of collider, and will most likely need a Rigidbody.
You will not want to set isTrigger as that will not make it physically react to a collision, but will just detect when a collision occurs. It will also not call OnCollisionEnter but will call OnTriggerEnter. Setting both not as triggers, adding a collider, and giving them Rigidbodies should allow the collision to be detected. You will also need to attach this script to one of the objects that have the collider. Are there other components on the objects you are using?
Make sure you are not looking for
void OnTriggerEnter(Collision collision){}

Unity prefabs acting differently based on whether dragging them into the scene statically or having them dynamically generated by code

If I instantiate a prefab like so:
GameObject asteroid = GameObject.Instantiate(thing2spawn);
//then set the location and some other stuff
The prefabbed asteroid's collisions do not register at all, but If I simply drag the asteroid into the scene, the collisions work exactly as expected.
Figured it out.
My asteroid object has two child objects which contain colliders that do different things. Hitting a certain part of the asteroid heals you, while hitting the other part damages you. These two children had rigid body components so when I set the speed of the asteroid (the parent object) in code, the two children wouldn't move with it. So it seemed like the colliders weren't working when really they were just left behind in some other place. Removing the rigid bodies from the children fixed the problem.
Weird.

Somehow physically colliding with Collision Triggers

With a whole bunch of copy-pasting from all over the web, I managed to hack together a piece of C# code which spawns a random object from an array when the player enters a large, spherical collision trigger. When the player leaves the trigger, the object is deleted. It's working almost as intended, only problem is that six out of ten times when the player passes through the collider, for a split second it feels like a physical collider. Meaning that although the player is trying to move straight forward, he or she will move left or right along the trigger collider for about half a second, before the object spawns and the player can move freely again.
I was under the impression that checking the "Is Trigger" box would remove all physical properties of that specific collider? Am I missing something?
I'm using the standard FPS Character Controller in Unity 5 if that might have something to do with it.
All help is greatly appreciated.
Make sure the spherical collision trigger has a rigid body component, and that the "Is Kinematic" property of the rigid body is checked.
Realized my solution didn't get posted as I thought it had. Yoyo's solution was correct. The object containing the code was also the trigger. Switched so that the player was the trigger and now all is well.

Categories