简体   繁体   中英

Starting a Particle System in Unity with Buttons

Hello everyone I made a rocket fire partical with Unity's partical system but Idk how can I make it activated when I press my thrust engine button (W and up Arrow is my buttons) This is my code;

public class Rocket : MonoBehaviour
{
    
    private Rigidbody2D rb;
    
    public float amount;
    public float MaxVelocity = 3;
    public float RotationSpeed = 3;

    




    // Start is called before the first frame update
    void Start()
    {
       rb = GetComponent<Rigidbody2D>();


    }

    public void ThrustForward(float amount)
    {
        Vector2 force = transform.up * amount;

        rb.AddForce(force);
        
   

    }

    // Update is called once per frame
    void Update()
    {
        float Yaxis = Input.GetAxis("Vertical");
        float Xaxis = Input.GetAxis("Horizontal");

       
        ThrustForward(Yaxis);
        Rotate(transform, Xaxis * RotationSpeed);
         

    }   
    private void ClampVelocity()
    {
        float x = Mathf.Clamp(rb.velocity.x, -MaxVelocity, MaxVelocity);
        float y = Mathf.Clamp(rb.velocity.y, -MaxVelocity, MaxVelocity);

        rb.velocity = new Vector2(x, y);
      

    }


   

    private void Rotate(Transform t,float amount)
    {
        t.Rotate(0, 0, amount);
     
    }
   

}

Please help me how can I start a particle system in unity with buttons Thanks!!

If You Have A Particle System, You Can Make A Variable In Your Code Like, Following:

public ParticleSystem particle;

You Can Play The Particle System By Using The Variable With .Play() in end, for example, if you have created a variable like above as I suggested, you can play the particle system whenever you want.

The Way We Do It Is Pretty Simple.
 particle.Play();


Direct Solution


Remove Everything From Your Script And Try Overwriting It With Following Code
public class Rocket : MonoBehaviour { private Rigidbody2D rb; public ParticleSystem particle; public float amount; public float MaxVelocity = 3; public float RotationSpeed = 3; // Start is called before the first frame update void Start() { rb = GetComponent<Rigidbody2D>(); } public void ThrustForward(float amount) { Vector2 force = transform.up * amount; rb.AddForce(force); particle.Play(); } // Update is called once per frame void Update() { float Yaxis = Input.GetAxis("Vertical"); float Xaxis = Input.GetAxis("Horizontal"); ThrustForward(Yaxis); Rotate(transform, Xaxis * RotationSpeed); // You Can Do Exactly Same Thing For Arrow Key // To Get Better Rusults Try Messing With The Particle System's Settings } private void ClampVelocity() { float x = Mathf.Clamp(rb.velocity.x, -MaxVelocity, MaxVelocity); float y = Mathf.Clamp(rb.velocity.y, -MaxVelocity, MaxVelocity); rb.velocity = new Vector2(x, y); } private void Rotate(Transform t,float amount) { t.Rotate(0, 0, amount); } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM