- Home /
multiple spheres spreads out
Hi. I'm new to unity and I'm trying to build a model like this.
http://www.zehnder-systems.com/international/comfosystems/Functional%20principle,11.html
I use sphere to represent the air bubble. I run into a problem that I don't know how to represent the effect when the air bubble comes out of the pipe. The air bubbles should spread out in all directions. How to implement this? I try to use addforce but it does not work well.
Answer by Wolfram · May 23, 2011 at 11:00 PM
Attach a script like this to each sphere:
var minVelocity:float=1.0;
var maxVelocity:float=2.0;
private var randomDir:Vector3;
private var randomVelocity:float;
function Start(){
randomDir=Vector3(Random.Range(-1, 1), Random.Range(-1, 1), Random.Range(-1, 1));
randomVelocity=Random.Range(minVelocity,maxVelocity);
randomDir*=randomVelocity/randomDir.magnitude;
}
function Update(){
transform.position+=randomDir*Time.deltaTime;
}
Answer by DaveA · May 23, 2011 at 11:26 PM
For the ones coming out, you could use a particle system and image of a bubble. But some of those go 'in' and it's tricky to get particles to do that.
For the 'in'-part, you could initialize your particles at random positions but with 0 velocity. Then manually update the particle positions:
function LateUpdate(){
var particles = particleEmitter.particles;
for (var i = 0; i < particles.Length; i++) {
particles[i].position = $$anonymous$$athf.Lerp(particles[i].position,
transform.position, Time.deltaTime);
}
// copy them back to the particle system
particleEmitter.particles = particles;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612083640im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Addforce.forward on a sphere object 1 Answer
Erratic behavior of sphere and AddForce? 1 Answer
re-orienting velocity 1 Answer