Rocket Seal 5000 - Controls Mapped to XBox One 🚀

uploaded a new video with better audio, the last one you couldn’t tell what I was saying.

public class PlayerController : MonoBehaviour{
Rigidbody rbPlayer;
//*I decided to use a multiplier variable for the moving the
//  rigidbody instead of only adjusting the mass.
public float thrusterForce = 100f;
public float horizontalForce = 150f;
AudioSource thrusterSound;

//these are to regulate the speed multiplier so the player does
//  not continue accelerating past a certain point. Max speed. 
public float moveSpeed = 0.8f;
public float maxSpeed = 0.8f;

// Start is called before the first frame update
void Start()
{
    //the get ONLY the component of type Rigidbody
    rbPlayer = GetComponent<Rigidbody>();


    thrusterSound = GetComponent<AudioSource>();
    thrusterSound.Stop();

}

//use FixedUpdate instead of Update any time physics are being altered 
// (such as velocity and torque). I just make it a habit to put controls in here instead.
void FixedUpdate()
{
    PlayerControls();
}

//private methods can only be called from our own code
private void PlayerControls()
{
    RocketThruster();
    Rotation();
}   

private void RocketThruster()
{
//GetKeyDown only activates the control you want once, even if key is continuously pressed down
//  for a few seconds or whatever. We want to use GetKey so that the whole time the button
//  or key is pressed down the rocket is being activated
if (Input.GetKey(KeyCode.Joystick1Button0))
{
    Debug.Log("Rocket Thrusters Activated - XBox");

    //using Vector3.up adds force on the Y axis of the object's local axis NO MATTER WHAT 
    //   DIRECTION IT 
    //  IS FACING. that is important. may have to mess with the MASS of the players ship*.
    rbPlayer.AddRelativeForce(Vector3.up * thrusterForce * Time.deltaTime * moveSpeed);

    //helps to regulate acceleration
    rbPlayer.drag = (moveSpeed / maxSpeed);

    //if thruster sound is NOT playing (! means 'is not")
    // this fixes that weird audio bug where it made an unpleasant sound when
    // the thruster button is held down. 
    if (!thrusterSound.isPlaying)
    {
        //plays SFX for thrusters
        thrusterSound.Play();
    }
    }
    else if (Input.GetKey(KeyCode.Space))
    {
        Debug.Log("Rocket Thrusters Activated - Keyboard");

        rbPlayer.AddRelativeForce(Vector3.up * thrusterForce * Time.deltaTime * moveSpeed);

        //helps to regulate acceleration
        rbPlayer.drag = (moveSpeed / maxSpeed);

        //plays SFX for thrusters
        thrusterSound.Play();

    }
    else
    {
        thrusterSound.Stop();
    }

}

private void Rotation()
{
    //making this a separate 'if' statement so that the player can use Rocket Thrusters
    //  while rotating
    //The 'A' key will take precedence over the 'D' key becuase it is listed first
    rbPlayer.freezeRotation = true; //take manual control of rotation

    if (Input.GetKey(KeyCode.A))
    {
        Debug.Log("Left Rotation Activated - Keyboard");

        transform.Rotate(Vector3.forward * horizontalForce * Time.deltaTime);

    }
    else if (Input.GetKey(KeyCode.D))
    {
        Debug.Log("Right Rotation Activated - Keyboard");

        transform.Rotate(-Vector3.forward * horizontalForce * Time.deltaTime);

    }
    //Xbox One Controls
    //when player pushes joystick to RIGHT
    else if (Input.GetAxis("XHorizontal") > 0)
    {
        Debug.Log("Rotating Horizontal - XBox");

        transform.Rotate(-Vector3.forward * horizontalForce * Time.deltaTime);
    }
    //when player pushes joystick to LEFT
    else if (Input.GetAxis("XHorizontal") < 0)
    {
        Debug.Log("Rotating Horizontal - XBox");

        transform.Rotate(Vector3.forward * horizontalForce * Time.deltaTime);
    }

    rbPlayer.freezeRotation = false; //resume physics control of rotation
}
2 Likes