I don’t know if it is the correct forum. But, I have an issue on how to interact with others objects with the player.
The game is non tiled 3D world.
For example, my player has an axe and I want to cut a tree to get wood. The tree has wood as reward.
Here is my issue, how to do the collisions.
I thought of having rigidbodies in the weapons and colliders on all objects in the world.
The tree receives an event from the collider, and if it a weapon type axe then destroy the tree and give rewards.
Another issue is the animation. I have the player with a swing animation. So, I suppose to attach the weapon in the player so it moves together. And I can change the weapon using the same animation.
well you would have to do what you pretty much said yourself put a rigidbody on the axe and tree and a collider on both. and make sure to have the correct colider like the tree should most likely be a cylinder, and the axe could be a sideways cylinder or a mesh collider. and once the axe and tree hit you collect wood. and obviously in the tree you would do as following
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.name == "tree" /* Could be tagged or you could directly get ObjectOfType*/)
{
// Do whatever you need to do like reward with logs. or something
}
}
thats how i would do it, obviously this would require tree’s to have a name like OakTree, or RedTree etc. so u can easily make different trees reward different things
well no since u can’t really get around it since if u want to interract with objects u kinda need colliders and rigidbody’s howver since i assume the tree isnt moving you could make the rigidbody static that requires least amount of resources,
Animated things and rigidbodies work kinda poorly together.
The animation wants to position the animated object. The rigidbody wants to be positioned by the physics engine. Those two are going to fight each other, and it’s going to look bad and be awkward.
You don’t need collisions here. You just want to check if you’re looking at a tree and if you’re in range of it.
For that, you really only need a raycast. The docs for raycast show how to check if something is in front of your mouse.
well Raycast is a bad thing if the Tree has alot of triangles since it would have to search for the one triangle that was hit. and also colliders can be used as a trigger to check if something triggered it instead of actually doing a collision.
i am pretty sure that many games that has functions where u can cut down trees .or mine rocks etc have a collider attached as a trigger. cause doing Raycast is to expensive. i might be wrong, but from what i read nearly everywhere. is that raycast isn’t cheap. and once again nearly no one from what i can read and see in games does raycasting to a object but they have trigger zones.
I suppose in both cases (Raycast and colliders) unity has to check against all colliders or objects in the scene to check collisions. And in both cases the main issue would be the events and the garbage collector. I should measure that.
I don’t know how Unity handles this. If it performs a Frustrum Culling, BVH or/and LOD automatic algorithms.
If I choose the easiest way I would use colliders as triggers.
That is the way I collect the items right now. But they are a few of them. Not an entire world.
The tree won’t have a mesh collider. That’d be crazy. You’d have a single capsule collider to match the shape of the tree. The raycast will both be the easiest with regards to animation and implementation. That’s from experience. Trigger enters are much more finicky, and also happens when you don’t care about them. A raycast only happens when you raycast.
Both raycasts and collisions happens in the physics engine, PhysX, which is heavily optimized. It’s a lot faster than checking against every collider in the scene. It also happens in c++, so no GC load happens. We do quite a few raycasts per frame to move characters around, and it’s not even visible in the profiler, it’s so far down the list.
My 2 cents on this topic… I’d say, with an axe and a tree it would be good to have a proximity area. This area tells you you’re close enough to begin your action. If you begin, perhaps you turn the character towards the target (possibly move a bit closer if the area is large), and then you begin your animation. However, the animation has nothing to do with the chopping of the tree; it’s only visual, and you continue your logic from there…
That’s what I’d do.
well as mentioned before Colliders doesn’t even check or update unless you enter their zone. but either way i would do Trigger zones or proxi zones which pretty much are the same. but ultimately it’s up to you…