- Home /
Fruit Ninja - Cut fruit effect
Hi fellas!
I want to make a game similar to Fruit Ninja and I don't really have an idea on how to create the effect of the fruit cut in half. I thought about removing the fruit object and instantiate 2 separate objects with texture of fruit cut in half on them but I'm not sure if it's a good way because I'm creating for iPhone. I'm afraid in the end, there will be too much objects on the screen (let's say if the player cut 5 fruits, there will be 10 objects on the screen) and it will cause the game to lag. I appreciate any tips and advice about this. Thank you in advance =)
Answer by mpavlinsky · Feb 16, 2012 at 02:55 PM
Well anyway you cut it (no pun intended) you're going to have to be able to display MaximumFruits*2 objects on your screen minimum. You can curb the load a little bit by making the halves static geometry and batching them since at least 2 will be the same (if the fruit is perfectly bisected) and they aren't animating. I honestly don't think the rendering is that big of an issue performance wise, I wouldn't instantiate them on the go as you suggest though. A better strategy would probably just be to define the maximum number of any given fruit that can be on the screen at once and then instantiate all the fruit halves for them when you load your scene and just don't render or use them until a player cuts a fruit.
I've actually tried instantiating multiple objects at once on the screen. I tried 15 objects (I pick 15 because it is so far the most amount of objects to appear on the screen) at once and no problem for iPad 2 and iPhone 4S. However, when tested on iPhone 3GS the game became lag.
I've tried combining 2 objects and position them to make them appear as a single object but it cannot be done because no matter how there will be a single line at the middle of the two objects when I applied the texture.
Having 15 objects is not hard on the 3GS or any other platform in general, you may want to profile your code and see what the root issue is. I'm sure you could improve your performance by optimizing scripts, reducing the quality of your models, and using more efficient rendering techniques depending on what the bottle neck is.
As far as combining the two objects I would ins$$anonymous$$d just have a model that is the whole fruit and instantiate as many of those as you need in using the same method I described above. Then just deactivate the whole fruit and move the two halves of fruit into place (rotate them correctly, set physics velocities, play a particle effect and do whatever else Fruit Ninja does) when the player swipes the fruit.
Yeah, my code is still messy because I need to finish the other core features in the prototype so I hope the optimization will pay off in the end.
I will try to use the method you suggest and I will give the update as soon as I can. Thanks for the tips =)
I've tried instantiating 2 objects and remove the slashed object and I think it is fine. Not really much performance issue even with crappy code. The hard part is only positioning the slashed parts properly and applying realistic movement.
Don't position them manually. make two empty objects as children of the main fruite and position them in a good way and then just instantiate new objects in their position. you can get their transform either by setting them in 2 variables in inspector or using a foreach on the transform component. the first way is the recommended one.
Answer by Ashkan_gc · Feb 16, 2012 at 05:42 PM
There is only one optimization in my mind at the moment. instantiate other objects with the main one and just set their gameObject.active property to false or just disable their rendering by setting renderer.enabled to false. You can even do this for the main fruites themselves. let's say this:
- Instantiate 5 Orange objects and 10 Orange parts if you at most show 10 of them at the same time.
- Then make all of them inactive by setting their active to false.
- when you want to instantiate a new orange just set active to true
- and when you want to destroy, just set it to false.
the technique is called polling and is very useful because you'll remove much of the overhead of instantiation. Also you can use Graphics.Drawmesh to draw meshes without having gameObjects but then you should calculate movements yourself and without using rigidbodies.
About alverndbl's answer i should say that it seems that he is talking about a solution that could cut meshes at runtime. it's really great for it's usage but doing it on the phone would kill your performance.
I remove the game objects when they got slashed, so how does setting the gameObject.active state to false differs from my current method?
I don't think I can use Graphics.Drawmesh because it is only for UnityPro according to the documentation =(
You should first position those inactive gameObjects acording to your main fruite and set their active to true. you should yourself maintain a list of those inactive gameObjects in a List and add and remove them in it. this way you'll not have the overhead of destroying. also ins$$anonymous$$d of destruction you can set active of the current main fruite to false and add it to the inactive list of the main fruite.
Answer by Alverndbl · Feb 16, 2012 at 12:33 PM
I think I saw something like this in the asset store. Had a webplayer demo too, can't remember the name sorry.
Actually it's Piece$$anonymous$$aker. There is one called Object Cutter as well, but as the single review there hypothesises it's probably of lesser quality.
Your answer
Follow this Question
Related Questions
How to have raindrops randomly instantiate from the sky 2D 1 Answer
Instantiate objects on iPhone affecting performance, strategy. 1 Answer
Attaching Object as child at certain point in animation 2 Answers
Is it possible to adjust an Objects pivot without affecting any of the child objects? 1 Answer
Can you animate instantiated prefabs? 0 Answers