![]() In Unity, every object can have one tag - 'weapon', for example, could be a tag. ![]() A better method is to use FindGameObjectsWithTag. The problem with this solution is that adding additional weapons makes the code grow exponentially - a third weapon would mean 8 more lines of code, a fourth would mean 14 more, and so on. This has a high overhead, but you only need to call it once at startup and the weapon is saved. As a test, I did this first manually, using gameObject.Find(), which finds a specific object by name. The difficult thing to do was finding the weapons. Before you press play though, in the editor, the weapons overlap. This meant that the weapon, it's components and its children were all 'off' in the game world. I positioned each weapon where it should be relative to the camera, then deactivated one of the weapons on startup using gameObject.SetActiveRecursively(false). The actual script to switch weapons wasn't very difficult at all to implement. If you want to reuse it, you have to save it as a variable. One thing about Unity scripting is that in order to interact with any game object (other than the one the script is actually attached to) is that you must 'find' the game object first. Yesterday I added the ability to switch weapons (again, models all from ).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |