I have added a particle system where I have checked the collider option and added a world particle collider. See the image below.
In the script that is attached to the particle system I have:
void OnParticleCollision(GameObject other) {
Debug.Log("Particle was hit!");
}
The bullets that are fired don´t seem to hit the particles since the above message is not printed. The bullets are spheres with a sphere collider and a rigidbody attached. The rigidbody is set to non-kinematic (the checkbox is not checked) if that matters.
Also, the bullet object has a script attached with the same lines as above:
void OnParticleCollision(GameObject other) {
Debug.Log("Bullet was hit!");
}
But it is not printed as well.
What am I missing?
Double check that you have this script attached to your particle system and not any arbitrary gameobject.
Check if you have "Is trigger" disabled on Sphere Collider, or you're particles too small and don't hit the actual collider.
Tried to simulate your situation, all works fine.
Related
I want to spawn several circles on the screen as game objects that float around randomly. To do this I have a prefab that I am instantiating x number of times in a script attached to a main game object. Then I have a 2nd script attached to the prefab to control the random movement. I added a Particle System to the prefab, so that each spawned clone has particles emitting from its edges. I want to know if one object's particles collide with anything, be it another cloned object, a wall, etc. But my OnParticleCollision (in the movement script attached to the prefab) is not logging anything to the console, it seems to not detect particle collisions at all. Maybe I'm not understanding the bigger concept and instantiating multiple instances of the same prefab with a particle system is not the best approach? Or am I making a more obvious minor mistake?
Things I have tried based on other questions:
Send collision messages IS checked
I do not have any colliders marked as triggers
I verified the visual bounds look correct in Scene View
Collision between cloned game objects themselves work fine, it's only the Particle Collisions not working.
My script attached to the prefab:
public class BubbleMove : MonoBehaviour
{
public Rigidbody2D rb;
void Start()
{
rb.velocity = new Vector2(min, max);
ParticleSystem ps = GetComponent<ParticleSystem>();
ps.transform.position = new Vector3(transform.position.x, transform.position.y, 0);
ps.Play();
}
// Update is called once per frame
void Update()
{
}
void OnParticleCollision(GameObject col){
Debug.Log("Collision Particle: " + col);
}
}
Images of my prefab inspector settings for Rigidbody2D, Circle Collider, and Particle System:
Hi I see the rigidbody is not defined in the inspector for the BubbleMove script.
According to this article is possible to solve collision problems with fast moving objects in unity.
If you have particle system firing 1 sphere particle (ball throw for example) with collider and gravity modifier. And another moving collider with rigidbody (baseball bat for example), is there any possibility to make these object to NOT going through when they will collide in "high speed"? You cannot set "collision detection" on particle.
For better understainding I am adding two screenshots bellow.
Feel free for asking any question.
Collider(wall) is not moving, particle will bounce
Collider(wall) is moving towards ball with speed of "swing", particle goes through
The problem is because your objects are moving at a speed that they go through the collider. Say your object would move 10 units a frame, and it was 1 unit away from the collider. If the collider was 5 units long, it wouldn’t stop the moving object. This is because the next update will push it too far ahead of the next object.
First, don’t use a particle system for this. Just use a Rigidbody, because it is easier to handle in these situations.
You could solve this by doing a sweep test. A sweep test checks for colliders, where the object is going. You could just stop it from moving at the point it hit.
Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
Sweep();
}
void Sweep()
{
RaycastHit hit;
if (Physics.SweepTest(rb.velocity, out hit, rb.velocity.magnitude))
{
rb.velocity = rb.velocity.normalized * hit.distance;
}
}
Make sure this script is on your ‘baseball’, and add force somewhere in this script. Make sure that you are not using particle system, but you are using Rigidbody.
This was untested; let me know if it doesn’t work or you get an error.
I wanted to make a shot with a particle system and if one of the particles collides with something, then the opponent should get damage. I use the trigger function because I want the particles to continue flying after colliding. And in case you are wondering why I don't use a raycast: if I work with a raycast, the opponent gets harm without the particles arriving.
My Code:
private void OnParticleTrigger()
{
if (!hitObjects.Contains(other.gameObject))
{
other.GetComponent<IDamageable>().GetDamage(PlayerScript.instance.damage);
hitObjects.Add(other.gameObject);
}
}
where I would like to have the opponent's collider later, I have already inserted "other"
Make the projectile a game object with a child particle system that gets triggered once the projectile hits a target
As you see in the screenshot. When the ball hits between two colliders, it should do what red arrows shows. Instead it does what blue arrow shows. All the boxes in the game are perfectly next to each other.(Used an algorithm to make it perfect.).
I am using unity's own physics system. I only have this piece of code for the ball :
void Start()
{
rb = transform.GetComponent<Rigidbody2D>();
rb.velocity = new Vector2(10.0f, -5.0f) * 41f * Time.deltaTime;
}
What "Ball" game object has;
Sphere Collider2D
Bounce physic in Collider2D {friction set to: 0, bounciness set to: 1}
Those are enough to achieve exactly the same physics of Ballz game.
When the ball hits an edge of a collider, one of these situation happens. This is okay, exactly what i want. But I do not want the same thing happen when the ball hits between two colliders.
I hope i'm clear enough.
function OnCollisionEnter(theCollision : Collision)
{
if(theCollision.gameObject.name=="Spotlight")
{
Destroy(gameObject);
Debug.Log("Dead");
dead = true;
}
}
This is my code here, I have a spotlight attached to my enemy which acts like a torch, what I want is for the enemies to be destroyed once they walk into the spotlight.
I tested the collider with the player and it works fine but for some reason, when I set it to the spotlight, nothing happens at all.
Can anyone help me out here?
You can ray cast from the origin of the light to your surface. Once you have found the intersection point of this ray with your surface you can control the XZ position of a capsule or sphere collider of size(radius, N, radius) - attach your detection script to that collider and you'll be able to work with it as if it were a physical object in the scene.