Runtime Tree Colliders - UnityContrib/framework GitHub Wiki

Runtime Tree Colliders

Introduction

This asset adds colliders to the trees that are near the player. Unity can also do this but there is a limit on how many colliders there can be in a scene at the same time. To work around this, this component recycles existing colliders for all the trees keeping the number of colliders used for trees to a minimum.

Use at own risk!

Features

  • New Multiple capsule colliders per tree
  • Low number of colliders for trees
  • Quadtree for great performance
  • Only changes colliders when player have moved
  • Easy to set up
  • Tested with 70.000 trees in a 2000x2000 area
  • Example scene included
  • Tree collision information

Limitations

  • The colliders does not move with the wind

Requirements

Setting Up

Before you can start using this component you must have a terrain in your scene. The trees added to the terrain must all contain a capsule collider. If you need more capsule colliders for a tree add them as direct child game objects to the tree.

  1. Make sure that the "Create Tree Collider" checkbox of the "Terrain Collider" component is not checked.
  2. Add "Runtime Tree Colliders" component using "Add Component" button, by dragging the component onto the terrain or by using the menu "Component->Physics->Runtime Tree Colliders".
  3. Assign "Player" field to the transform that the colliders must follow around.
  4. Configure "Diagonal Length" and "Max Colliders" if needed.
  5. Run the game.

API

Methods

void TreeAdded(TreeInstance treeInstance)

Call this method when a tree is added at runtime.

void TreeRemoved(TreeInstance treeInstance)

Call this method when a tree is removed at runtime.

Fields

float diagonalLength

This is the length of the diagonal that defines the square surrounding the player. If a tree is inside this square it will get a collider.

int maxColliders

This number represents the maximum number of colliders this component is allowed to create. If you have multiple instances of this component each component may create this number of colliders.

Transform player

The transform that the colliders must follow around.

Changes

2014/12/03

Enabled use of multiple capsule colliders for each tree Updated to use assemblies

2014/09/12

Initial release

Troubleshooting

The console shows the error message "Could not create tree colliders. Maybe there are more Trees then PhysX can handle?"

You have forgotten to uncheck "Create Tree Collider" in the "Terrain Collider" component. Uncheck it and try again.

I can walk through the trees.

Verify that..

.. "Runtime Tree Colliders" component is added to the terrain in question.

.. the component is enabled.

.. the terrain trees has capsule colliders.

.. the player has a collider.

.. the player is moved using physics.

The game lags only when "Runtime Tree Colliders" component is enabled

Check that you have low numbers configured for "Diagonal Length" (default: 10) and "Max Colliders" (default: 20). Both numbers can together result in some very bad performance if not configured properly.

FAQ

Q: Can I edit the code?

A: If you have a working computer and internet then yes. The source code is available from github.

Q: How do I use the component in a network multi player scenario?

A: Only add the local player to the component's Player field.

Q: How do I use the component in a split screen multiplayer scenario?

A: This scenario is not supported very well. You can add multiple instances of this component to the same terrain and assign a different player to each component.

Q: Will you make a Unity Script/Boo version available?

A: No

Q: Will it work with Speedtrees?

A: Yes

Q: How do I know which tree the player collided with?

A: The game object containing the collider for a tree also has a "TreeInstanceInfo" component. This component stores a reference to the tree instance the collider is representing and a reference to the terrain. So when using OnCollisionEnter/Stay/Leave or raycasting you will get a reference to the game object and then you will be able to get the instance of the "TreeInstanceInfo" component. Have a look at Reposition.cs located in the Example folder to see an example.

Links

YouTube: https://www.youtube.com/playlist?list=PLNiMb6r5cNRNFfH0dIDC2mq1Kp7sFrPiD