Ok, I have this main class called Enemy, and inside it I have subclasses of different enemies (ie ZombieEnemy). I need a way to target all sprites/subclasses of Enemy. Ie, for collision detection, I need a way to see if ALL Enemy's are 'dead' to end the Level.
Thanks
There are plenty of ways to do this. One is to add a method to your Enemy class like -(BOOL)isEnemy that simply returns YES. (That'd actually be more useful if Enemy has a superclass that you can customize, like GameObject. Implement -isEnemy in that class to return NO. Otherwise, you won't know if you can call -isEnemy on a given object.) Subclasses will automatically inherit this method. Alternately, you could test the class of each object using -isKindOfClass:. Or, since you're the one creating enemies, you could certainly keep a list of all active enemies. This is probably the best plan if you have lots of objects on the screen, only some of which are Enemy objects.
Deciding when all enemies are dead is something you probably want to do very often. It might make sense to keep a list of live enemies. When an enemy dies, remove it from the list. You can quickly test whether the player has successfully cleared the level by checking the length of the live enemies list. If it's greater than 0, there's more work to do.
Related
I want to create a game with tons of different enemies, items, accessories, heroes, attacks and more.
To make it easily expandable and maintenance, I want to create the game in the most modular way possible so nothing in the game will be built-in and every aspect of the game will be assembled and configured.
But I'm undecided as to how the elements should be modeled:
As ScriptableObjects - (for example) every enemy will be configured and represented by some scriptabe-object and this asset will contains the enemy stats and behaviours but also the enemy prefabs so the script will tell us how to create the enemy object in the game.
so if I have some place that I want to put some enemy in it, I should (for example) attach on the inspector the enemy's scriptable-object.
As Prefabs - every enemy will be represented by some prefab and the prefab root object will contains some scriptable-object that its purpose is to hold some specific enemy stats and behaviours and the enemy will be act according to them.
so if I have some place that I want to put some enemy in it, I should attach on the inspector the enemy's prefab.
Is there some best practice for that? a way that will make my life easier when the game will be particularly large and complex?
I made two game objects, player and enemy.
I hope two game objects collision detect and they are pass through each other.
Thus i put a check mark objects collider's 'is Trigger' and objects's 'rigid body'. So, i expected that objects are pass through each other.
But, player and enemy crashed each other. Why player and enemy crash?
i want two objects pass through each other.
help me please.
A collider marked as a trigger should not cause physical collisions with an incoming Rigidbody (see here). Check and see if one of the player or any objects has a child with a collider that is not marked as a trigger.
Also note that a CharacterController component will be constrained by collisions, as mentioned in the documentation. CharacterControllers do not have a trigger option, so you will have to disable collisions through a different method. The most common methods are using Physics.IgnoreCollision or changing the layers of the objects and disabling collisions between layers in Edit -> Project Settings -> Physics. See here for more details: Layer-based collision detection and How to Ignore Collision between Objects.
I created several objects in a blender: man, horse, dog, and others. I can animate them separately, like walking or running but I can not understand how to make the interaction animation between them in Unity, like riding a horse or pet a dog. To which object to apply this animation? Can you please describe or provide some tutorials for me?
I don't need already done solution, I want to realize how to make this on my own.
There are several ways to do that.
The easier way to do that is to include a script with public GameObject (consumers) variables in a GameObject (producer). With that reference, you can interact with other GameObjects.
You can drop the reference of the GamObject(consumers) on these public variables. These consumers will react to an action of the producer GameObject script.
Example public GameObject
However, I would really recommend you to use Events, it is a more elegant way to do the interaction between objects.
Basically, one of the objects triggers an Event and the subscribers (other objects) will react in consequence to this event.
I'm making a level based game using SpriteKit. I would like to know the best practice for making level changes. I was originally using one Scene as my gameplay scene and when a level is completed, it removes all nodes in the scene and then adds the ones for the next level. I am using a background node that is persistent throughout the entire course of the game. I'm worried about memory because I don't think ARC will deallocate the nodes removed because the scene is persistent. Is this method ok or should I instantiate a new scene for when a level is changing?
The best practice is to separate game data and assets from game code. This way, you can make changes to stuff without having to recompile (and is handy if you are working with another person who doesn't code).
Here is what apple has to say about it at WWDC 2014:
When you transition between scenes, ARC will deallocate the prior scene, assuming you made no strong references to it. Since you're starting out it's unlikely you need to worry about this right now... it's mostly done with globals and closures, and you should be able to fix it if it becomes a problem (but likely won't).
You can use things like unowned self and weak var etc to ensure this doesn't happen when needed. Again this is a more advanced topic but it's good to be aware of them.
Really, making level changes is entirely up to you... if you want to make a state manager that swaps out stuff to one scene, you can certainly do that... or, you could make a bunch of scenes and transition to that. Again, the best practice here is separating game content from game code, not necessarily how you switch scenes.
Personally, I would used separate SKScenes-- because it's already built in with transitions, memory management--and you have the option of using the editor, and you get to give each scene it's own file if desired.
There is also GameplayKit which has a statemanager, in which case you could use one scene and have different states be the level.
here are some resources, buried in there are some nuggets pertaining to what you want.
https://developer.apple.com/library/content/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/DesigningGameswithSpriteKit/DesigningGameswithSpriteKit.html
https://developer.apple.com/videos/play/wwdc2014/608/
https://developer.apple.com/library/content/samplecode/DemoBots/Listings/DemoBots_SceneManager_swift.html
I am currently making a simple side-scroller. In unity. I am looking for a way to spawn objects before they enter camera, and remove them after they have passed the camera. All this will happn in the x axis.
There are 6 different kinds of objects that are going to be spawned. and with different distence from each other.
The objects can be picked up, and they are all prefabs and have the functional scripts for pickup if the player collide. But I need a way to remove them if they didn't get picked up.
Is there anyone that have a simple script where this will be possible?
there is a 2 methods in MonoBehaviour you can use: OnBecameInvisible and
OnBecameVisible. they are called whenever the renderer is no longer visible (or becone visible) by any camera.