Im trying to develop a control shceme that can be changed after a button press

Im trying to map movement directions to specific keys for a playerController script and then I’m going to create a second script with a different control scheme. Each script is activated and deactivated by a specific button. My problem though is im only seeing a way to transform a player forwards and backwards not left and right. Basically I’m wondering if there is a way to use transform.forward on the y-axis as opposed to the z-axis. here is the code Im using so far.

if (Input.GetKeyDown (KeyCode.W))
{
rb.velocity = transform.forward * speed;
}
if (Input.GetKeyDown (KeyCode.S))
{
rb.velocity = -transform.forward * speed;

Hey there

I think something like this should work:

If(Input.GetKeyDown(KeyCode.A))
{
rb.velcoity = transform.right * speed;
}

If(Input.GetKey(KeyCode.D))
{
rb.velocity = -transform.right * speed;
}

But I would not make serveral scripts, I rather would make one script, with serveral if conditions and an integer/counter…

if(counter == 1)
{
...
}
else if(counter == 2)
{
...
}

But have a nice day, good luck,

WbrJr

You keep changing your question as I read your post!

  • Do you need a “control scheme that can be changed after a button press”?
  • You don’t know how to apply force to left and right?
  • You need to know if there is a way to use transform.forward on the y-axis as opposed to the z-axis?

in case your question is the first one, here is an answer:
You can use the Strategy Pattern like so:

public class PlayerController : MonoBehaviour
{
    Movement movement;
    float speed;

    void Start()
    {
        movement = new FirstMovement();
    }

    void Update()
    {
        rb.velocity = movement.Force(transform, speed);
    }

    public void ChangeMovement(Movement m)
    {
        movement = m;
    }
}

public interface Movement
{
    Vector3 Force(Transform transform, float speed);
}

public class FirstMovement : Movement
{
    public Vector3 Force(Transform transform, float speed)
    {
        Vector3 force = Vector3.zero;

        if (Input.GetKeyDown(KeyCode.W))
            force = transform.forward * speed;

        if (Input.GetKeyDown(KeyCode.S))
            force = -transform.forward * speed;

        if (Input.GetKeyDown(KeyCode.D))
            force = transform.right * speed;

        if (Input.GetKeyDown(KeyCode.A))
            force = -transform.right * speed;

        return force;
    }
}

public class SecondMovement : Movement
{
    public Vector3 Force(Transform transform, float speed)
    {
        Vector3 force = Vector3.zero;

        if (Input.GetKeyDown(KeyCode.W))
            force = transform.up * speed;

        if (Input.GetKeyDown(KeyCode.S))
            force = -transform.up * speed;

        if (Input.GetKeyDown(KeyCode.D))
            force = transform.right * speed;

        if (Input.GetKeyDown(KeyCode.A))
            force = -transform.right * speed;

        return force;
    }
}