Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Zackton · Nov 11, 2015 at 05:49 PM · movementcharactercontrollermoveinput.getaxis2 player

Using Input.GetAxis on a 2 Player Game

Hello,

I have been attempting to use CharacterControllers on my two player Shoot 'Em Up game, and thought I may as well use CharacterController.Move to move the players around the field.

I had a look at the documentation on this, and have implemented the example code into my code. The only problem is, it uses Input.GetAxis Horizontal and Vertical, so when I move one player, the other one will move as well (It happens currently when both player movement keys are pressed, separately it works fine as the if statement handles that).

Is there a way I can handle both players using the GetAxis Horizontal and Vertical? I have tried every second Update() to move one character alternatively, and have also rewritten the movement so that if W is pressed, transform.position.x + 1 * speed * Time.deltaTime etc, but this keeps moving the player in the direction I pressed until I press another key, then the character slows down the goes in the opposite direction (may be my error in coding but all looked good), so I would much prefer to have the GetAxis Horizontal and Vertical working instead. Here is my code below:

 //Player starting position
 public Vector3 playerTwoPosition = new Vector3(0, 0, 0);
 
 //The CharacterControllers
 CharacterController playerOne;
 CharacterController playerTwo;
 
 public float speed = 10.0f;
 
 void Start() {
     playerOne = GameObject.FindGameObjectWithTag("Player 1").GetComponent<CharacterController>();
     playerTwo = GameObject.FindGameObjectWithTag("Player 2").GetComponent<CharacterController>();
 }
 
 void Update() {
     if (this.gameObject.tag == "Player 1") {
         if (Input.GetKey("w") || Input.GetKey("a") || Input.GetKey("s") || Input.GetKey("d")) {
             playerOnePosition = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
             playerOnePosition = transform.TransformDirection(playerOnePosition);
             playerOnePosition *= speed;
         }
     }
 
     if (this.gameObject.tag == "Player 2") {
         if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.RightArrow)) {
             playerTwoPosition = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
             playerTwoPosition = transform.TransformDirection(playerTwoPosition);
             playerTwoPosition *= speed;
         }
     }
 
     playerOne.Move(playerOnePosition * Time.deltaTime);
     playerTwo.Move(playerTwoPosition * Time.deltaTime);
 }

Thank you in advance for you help and insight, Daniel.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Soos621 · Nov 11, 2015 at 06:36 PM 0
Share

I'm adding this as a comment because its really just a suggestion, but you can use photon from the asset store

https://www.assetstore.unity3d.com/en/#!/content/1786

this will allow you to set up your "players" and objects per player instance.

heres a tutorial on photon https://www.youtube.com/watch?v=lrk7GJUp$$anonymous$$ns

2 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by tanoshimi · Nov 11, 2015 at 06:48 PM

You need to define additional axes in the Input Manager, then simply :

 if(Input.GetAxis("P1_horizontal")) 
   // move P1
 if(Input.GetAxis("P2_horizontal")) 
   // move P2
 Etc.


http://docs.unity3d.com/Manual/class-InputManager.html

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zackton · Nov 12, 2015 at 06:16 AM 0
Share

Thank you so much @tanoshimi for getting back to me so fast. I can't believe I didn't think of this myself. It works perfectly.

avatar image Zackton · Nov 13, 2015 at 02:36 AM 0
Share

Ahh, I can't believe I never thought of that! Thank you @tanoshimi so much for the fast response, sorry I couldn't thank you earlier. Works perfectly now :)

avatar image
0

Answer by PRABHBIR123 · Feb 12, 2019 at 02:48 AM

Hey @Zackton check out this tutorial.

https://youtu.be/UPQ--4dtmEM

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Two near-identical scripts cause a cat to jump in different ways - why? 2 Answers

controller.Move not working correctly 0 Answers

Problem CC and plane 0 Answers

Move in the direction its facing C# 2 Answers

CharacterCollider.Move always goes up!?!?! 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges