On pressing D both the thruster are being enabled rather than only left

There’s an issue where both the thrusters enable while pressing D

Rigidbody rb;
     AudioSource audioSource;
     
    [SerializeField] float mainThrust = 1000f;
    [SerializeField] float rotationSpeed = 0f;
    [SerializeField] AudioClip mainEngine;

    [SerializeField]ParticleSystem thrusterParticle;
    [SerializeField]ParticleSystem leftThrusterParticle;
    [SerializeField]ParticleSystem rightThrusterParticle;
  
    
    // Start is called before the first frame update
    void Start()
    {
        
        rb = GetComponent<Rigidbody>();
        audioSource = GetComponent<AudioSource>();
    
    }

    // Update is called once per frame
    void Update()
    {
        ProcessThrust();
        ProcessRotation();
    }
    
    void ProcessThrust()
    {
        
        if (Input.GetKey(KeyCode.W))
        {
            rb.AddRelativeForce(Vector3.up *mainThrust*Time.deltaTime);
            if (!audioSource.isPlaying)
            {

                audioSource.PlayOneShot(mainEngine);
                
            }
            if(!thrusterParticle.isPlaying)
            {
                thrusterParticle.Play();

            }
           
        }
        else
        {
            audioSource.Stop();
            thrusterParticle.Stop();

        }

    }
    
    void ProcessRotation()
    {
       if (!rightThrusterParticle.isPlaying)
        {
            rightThrusterParticle.Play();
        }
               
        {
            rb.freezeRotation = true;
        }
        
        if (Input.GetKey(KeyCode.A))
        {
            transform.Rotate(Vector3.forward*rotationSpeed*Time.deltaTime);
        }
  
        else if (Input.GetKey(KeyCode.D))
         {
             transform.Rotate(-Vector3.forward*rotationSpeed*Time.deltaTime);
             rb.freezeRotation = false;
         if (!leftThrusterParticle.isPlaying)
         {
            leftThrusterParticle.Play();
         }

         }
         else
         {
         rightThrusterParticle.Stop();
         leftThrusterParticle.Stop();
         }      
    }
}

You have added screenshots, so it’s difficult to copy and paste the issue.

See this on code formatting in the forum

image
This issue is here. There is no if statement here at the top, so this code always runs

1 Like

@bixarrio Hi i dont understand what i have to add in the if statement i have updated the post can you explain like im a 3 year old?

I will make comments in the code

void ProcessRotation()
{
    // This plays the right particle if it isn't playing. This happens on every frame
    if (!rightThrusterParticle.isPlaying)
    {
        rightThrusterParticle.Play();
    }

    // This happens on every frame
    {
        rb.freezeRotation = true;
    }

    // If the 'A' key is pressed, do something
    if (Input.GetKey(KeyCode.A))
    {
        transform.Rotate(Vector3.forward * rotationSpeed * Time.deltaTime);
    }

    // If the 'A' key is not pressed but the 'D' key is pressed, do something
    else if (Input.GetKey(KeyCode.D))
    {
        transform.Rotate(-Vector3.forward * rotationSpeed * Time.deltaTime);
        rb.freezeRotation = false;
        if (!leftThrusterParticle.isPlaying)
        {
            leftThrusterParticle.Play();
        }
    }

    // If the 'A' key is not pressed and the 'D' key is not pressed, turn the thrusters off
    else
    {
        rightThrusterParticle.Stop();
        leftThrusterParticle.Stop();
    }
}

So, what’s happening is you are turning the right thruster on on every frame, but you only turn it off again on that frame if the ‘A’ or ‘D’ key was not pressed. It stays on if you press ‘D’

Judging by the code, I suspect it is supposed to look something like this

void ProcessRotation()
{
    if (Input.GetKey(KeyCode.A))
    {
        transform.Rotate(Vector3.forward * rotationSpeed * Time.deltaTime);
        rb.freezeRotation = false;
        if (!rightThrusterParticle.isPlaying)
        {
            rightThrusterParticle.Play();
        }
    }

    else if (Input.GetKey(KeyCode.D))
    {
        transform.Rotate(-Vector3.forward * rotationSpeed * Time.deltaTime);
        rb.freezeRotation = false;
        if (!leftThrusterParticle.isPlaying)
        {
            leftThrusterParticle.Play();
        }
    }

    else
    {
        rb.freezeRotation = true;
        rightThrusterParticle.Stop();
        leftThrusterParticle.Stop();
    }
}
1 Like

@bixarrio Thank you soo much the script is working as it should, soo the only issue in the script was the placement of the if (Input.GetKey(KeyCode.A)) needed to be at the start of the void ProcessRotation()?

It looks that way, @Imadity. The right thruster code was always running, whether you pressed ‘A’ or not. Glad it’s sorted now

Thanks again for the help you have been really helpful every time I have had an issue

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms