Search Unity

[SOLVED] 2 Player controls?

Discussion in 'Editor & General Support' started by benjamin.magnussen, Oct 23, 2009.

  1. benjamin.magnussen

    benjamin.magnussen

    Joined:
    Oct 22, 2009
    Posts:
    8
    I'm a student doing a small school project game.
    I have very little experience in scripting, so I'm using tutorial scripts for pretty much everything.

    I've used the 2d platform controller to setup 2 characters in my game. I've made alternate inputs for moving and jumping, but how do I connect these in the script? Right now I move both characters simultaneously.
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    If you've already used the Input manager to set up new input axes, you will note that the new axes have different names. The built-in left-right axis is called "Horizontal" - for the second player, it might be called "Horizontal2", say.

    In your code, you just need to look for the Input.GetAxis calls and replace the existing axis name with the new one for the second player. So, if your player 1 script has something like:-
    Code (csharp):
    1. var x = Input.GetAxis("Horizontal");
    ...then your player 2 script might have:-
    Code (csharp):
    1. var x = Input.GetAxis("Horizontal2");
     
    dilaratuluce and exe_cutable like this.
  3. benjamin.magnussen

    benjamin.magnussen

    Joined:
    Oct 22, 2009
    Posts:
    8
    Well thank you andeeee.

    I guess it was too obvious for me to see.
     
  4. ProgrammeJava

    ProgrammeJava

    Joined:
    Jan 18, 2016
    Posts:
    2
    @andeeeee Do we make separate scripts? I'm using 3d ThirdPersonController,
    so would I duplicate the script and change that?
     
  5. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    426
    No, just a case of creating an integer for playnumber, and then change the inputs to the playernumber accordingly.