0
\$\begingroup\$

I'm writing a C# player script for a class and when I run, the sound inputs work fine, but the movement inputs don't respond, and I don't know how to fix it.

Code:

Rigidbody rigidbody;
AudioSource audioSource;

void Start(){
    rigidbody = GetComponent<Rigidbody>();
    audioSource = GetComponent<AudioSource>();
}

void Update(){
    ProcessInputs();
}
private void ProcessInputs()
{

    if (Input.GetKey(KeyCode.Space))
    {
        rigidbody.AddRelativeForce(Vector3.up);
        if (!audioSource.isPlaying)
        {
            audioSource.Play();
        }
    }
    else
    {
        audioSource.Stop();
    }
    if (Input.GetKey(KeyCode.A))
    {
        transform.Rotate(Vector3.forward);
    }
    if (Input.GetKey(KeyCode.D))
    {
        transform.Rotate(-Vector3.forward);
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ From what I know of unity, it is generally suggested to not mix adding forces to rigid bodies and manipulating the transform like it is done here. Perhaps using only one approach could help in this situation. \$\endgroup\$ – Alexandre Vaillancourt Feb 7 at 0:30
0
\$\begingroup\$

You have to multiply your vectors by a certain value. This value could represent a speed, distance, amount, or force.

So for example,

rigidbody.AddRelativeForce(Vector3.up);

would be

 rigidbody.AddRelativeForce(Vector3.up * 5);

and

transform.Rotate(Vector3.forward);

would be

transform.Rotate(Vector3.forward * 10);
\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.