Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Moving Particles without a rigidbody

Discussion in 'Scripting' started by bronxbomber92, May 6, 2007.

  1. bronxbomber92

    bronxbomber92

    Joined:
    Nov 11, 2006
    Posts:
    888
    I know the way I'm moving the position of where the particles spawn has to be wrong. I'm attaching a rigidbody to the prefab that contains my particle system. I attach a script that contains this code for moving the particles.
    Code (csharp):
    1. Rigidbody newBullet = (Rigidbody)Instantiate( particleBullet, transform.position, transform.rotation );
    2.             newBullet.velocity = transform.TransformDirection( new Vector3( 0, 0, -initialSpeed ) );
    Obviously this isn't the best way, and probably not the fastest. What is a better way to achieve the same effect (I tried looping through all the particles of the particleEmitter and changing the velocity like above, but that didn't do anything)?

    Thanks,
    Jedd
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,758
    I'm not sure I follow what you're trying to do here. Is a bullet a particle, or does it emit particles?
     
  3. bronxbomber92

    bronxbomber92

    Joined:
    Nov 11, 2006
    Posts:
    888
    sorry, newBullet is the prefab that contains the particle system and the rigidbody.
     
  4. VICTOM

    VICTOM

    Joined:
    Aug 28, 2005
    Posts:
    233
    I think you're looking for

    Code (csharp):
    1. var force : float = 100.0;
    2. var newBullet : GameObject = Instantiate(Bullet, transform.position, transform.rotation);
    3. newBullet.rigidbody.AddRelativeForce(newBullet.transform.forward*force, ForceMode.Impulse);
    4.  
    Set your particle emitter to "Simulate in Worldspace" true and the particles will spray off the bullet, else they will travel with the bullet - depends on your desired effect.


    Cheers,
     
  5. bronxbomber92

    bronxbomber92

    Joined:
    Nov 11, 2006
    Posts:
    888
    Well, the whole point was to achieve the same effect I have, without the rigidbody. The particle system is the bullet, so basically I have rigidibody attached to a particle system, which doesn't make much sense :p
     
  6. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    Nothing particularly wrong with having particle effects with rigid bodies. All the bullets in my game Phoenix Final work in that way.

    If you really don't want a rigid body, write a script which uses Transform.Translate() to move the object around instead.
     
  7. bronxbomber92

    bronxbomber92

    Joined:
    Nov 11, 2006
    Posts:
    888
    The only reason I don't want it is because I thought having a rigidbody would be less efficent. Maybe the cost isn't that great?
     
  8. VICTOM

    VICTOM

    Joined:
    Aug 28, 2005
    Posts:
    233
    RB's are not a problem. It's collision testing and raycasting that you want to reduce. With bullets you're going to want to raycast. Any GameObject that moves a large distance per game step will/may pass straight through your collision test. Raycasting isn't too expensive but you still need to use it wisely.
     
  9. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    Well, you're trading off the (relatively small) overhead of the native code physics engine against the (relatively small) overhead of a Mono script. I haven't measured the difference, so I don't know which is really most efficient, but my point is that both features have some degree of cost. There's no completely free way of moving an object. You might as well use whichever's easiest, instead.