- Home /
Blood covering ground in 2D platformer
Hello,
I have just started my adventure with Unity so my question may seem silly to experienced users but anyway I'll give it a try. If you could just give me couple of keywords I could search for in order to solve my problem I would be very grateful.
As a sample project I wanted to create 2D pixel platformer, something like Contra. All went well up to the moment when I wanted to add some blood dripping from my enemies. I got stuck on generating the blood particles and simulating them painting over the ground. The effect I want to achieve looks like this: (live example: http://jsfiddle.net/0pg0LpL1/4/embedded/result/ , animated gif: http://imgur.com/oTeDydV)
I am wondering what is the best approach to achieve such effect. I thought about generating the texture of blood stains on the fly and placing it before camera. It would look like this: scene < blood texture < camera. The blood texture would be generated from lines drawn by "flying" blood particles masked by the ground/platforms texture:
Blood particle traces: http://imgur.com/a/0iIeQ
Ground mask: http://imgur.com/a/YKvCK
Masked traces (what I call blood texture): http://imgur.com/a/M8z2v
What is the best approach to achieve such effect? I know that generating texture with Texture2D.SetPixels on each frame is not optimal. I also see challenge in having a blood texture large enough to span over entire level to keep memory of all the blood spilled. Also the solution with one blood texture will not work if I wanted to have moving platforms that should also be painted.
Thanks in advance for any suggestions.
Answer by koslovdenis · Jun 15, 2017 at 09:53 AM
Did you manage to solve it? I've got the same issue, and I am now thinking about how to realize the same effect in my 2D platformer. I was thinking about 2D physics, creating little objects just the same way as particles splash out. But I can't imagine how that's supposed to work yet. IT's a pity particles in Unity doesn't have physics. :/
Hi @koslovdenis. Unfortunately no, I did not find any good solution. Let me know if you manage to produce this kind of effect.
Hi 45325426243, I finally got the way how to make it more or less like that. I used particles and I was wrong that it doesn't have physics though - it does, you just have to enable it in particle editor. So it can stick to walls and at other colliders. You can set up the lifetime of the blood and the gravity scale. It worked for me just the best ) I've got a nice horror slashy effect. I am attaching the tutorial that helped me to make a nice start with that. Good luck with your project ;)
https://www.youtube.com/watch?v=ge9X$$anonymous$$r$$anonymous$$x8m8
Answer by E_101 · Jan 18, 2018 at 06:45 AM
Hello, I am a novice code writer and am new to Unity but I recently created some code that might be able to help. Also, I have only tested this method on flat horizontal surfaces and it may not work exactly to individual specifications.
After creating your particle system, adding this code to a New Script (C#), and a new sprite for the blood stains you want to create on particle impact; select the sprite under the "Sprite Blood Stain" game object selector.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BloodStain : MonoBehaviour {
public ParticleSystem part;
public List<ParticleCollisionEvent> collisionEvents;
public GameObject spriteBloodStain;
// Use this for initialization
void Start () {
part = GetComponent<ParticleSystem>();
collisionEvents = new List<ParticleCollisionEvent> ();
}
// Update is called once per frame
void OnParticleCollision(GameObject other) {
int i = 0;
int numCollisionEvents = part.GetCollisionEvents(other, collisionEvents);
print (numCollisionEvents);
//Collision detection of particles on surface, returns location
Vector3 locOfParticleCollision = collisionEvents[i].intersection;
//(*SpriteBeingUsed*), (*LocationCoordinatesToSpawnSpriteAt*), (*RotationData*)
Instantiate (spriteBloodStain, locOfParticleCollision, Quaternion.identity);
}
}
There is certainly a better way to do this, however hopefully this has solved other's issues regarding the same question.