- Home /
Question by
Lukewarmsoda · Jul 28, 2019 at 06:32 PM ·
instantiate2d-physicsforcefluid
Location of Instaniation drifts away from the parent when a high force is applied
I am trying to make a 2D fluid simulation, and the droplet particles are getting instantiated away from the parent when a high force, in this script a strength of 60 or higher, is applied to them. The Scene only has a well sprite with the WellSpawner script attached, and an example droplet sprite with a Rigidbody2d with no gravity and Circle Collider2d attached. What's the problem? How do I fix it?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WellSpawner : MonoBehaviour
{
public Transform WellSource;
public GameObject DropletSource;
public int MaxCount = 100;
public int Lifetime = 100;
public float Strength = 10;
// Update is called once per frame
void FixedUpdate()
{
//Limits the amount of droplets
if (WellSource.childCount < MaxCount)
{
Instantiate(DropletSource, WellSource);
//Get the instantiated droplet's Rigidbody
Transform LastChild = transform.GetChild(transform.childCount - 1);
Rigidbody2D rb2d = LastChild.GetComponent<Rigidbody2D>();
//Apply a force in a random direction
float Angle = Random.Range(0f,360f);
rb2d.AddForce(new Vector2(Strength * Mathf.Cos(Angle),Strength * Mathf.Sin(Angle)));
//Destroy the droplet after some time
Destroy(LastChild.gameObject, Lifetime);
}
}
}
Comment
Best Answer
Answer by Lukewarmsoda · Jul 29, 2019 at 12:14 AM
I figured it out. I just replaced
Instantiate(DropletSource, WellSource);
with
Instantiate(DropletSource, WellSource.position, WellSource.rotation, WellSource);
Your answer
Follow this Question
Related Questions
How do you use Rigidbody2D.AddForce 1 Answer
Stacked Bullets on Instantiation 0 Answers
Acceleration and Max Speed on 2D Object 0 Answers
adding force to instantiated ragdoll 4 Answers
How to Instantiate a Gameobject with force. Please Help 1 Answer