Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by EClancy · Aug 27, 2014 at 03:44 AM · javascriptgameobjectplayercontroller

Player 1 and Player 2 move together

I have a separate script for player 1 and player 2, but they act like they are both interpreting the same controls from my one controller. When I take the P2 script off the second character, player 2 will not move. If I take the Player 1 script off of player 1, player 2 still moves.

Here is my Player 1 script:

  // Determine the angle of the left thumbstick
  var xAxis = Input.GetAxis("L_XAxis_1");
  var yAxis = Input.GetAxis("L_YAxis_1");
  
  //reset rotation so rigidbodies don't get flipped
  transform.rotation = Quaternion.identity;
  
  //find the target to move towards
  var xtarget = transform.position.x + xAxis;
  var ytarget = transform.position.y + yAxis;
  var target = Vector3(xtarget, ytarget, 0);
  
  // The step size is equal to speed times frame time.
  //This finds how hard the player is pushing the joystick, and changes speed
  speed = Vector2(xAxis, yAxis).magnitude * 4 ;
  var step = speed * Time.deltaTime;
  
  // Move our position a step closer to the target.
  transform.position = Vector3.MoveTowards(transform.position, target, step);

And here is my Player 2 script:

  // Determine the angle of the left thumbstick
  var xAxis = Input.GetAxis("L_XAxis_2");
  var yAxis = Input.GetAxis("L_YAxis_2");
  
  //reset rotation so rigidbodies don't get flipped
  transform.rotation = Quaternion.identity;
  
  //find the target to move towards
  var xtarget = transform.position.x + xAxis;
  var ytarget = transform.position.y + yAxis;
  var target = Vector3(xtarget, ytarget, 0);
  
  // The step size is equal to speed times frame time.
  //This finds how hard the player is pushing the joystick, and changes speed
  speed = Vector2(xAxis, yAxis).magnitude * 4 ;
  var step = speed * Time.deltaTime;
  
  // Move our position a step closer to the target.
  transform.position = Vector3.MoveTowards(transform.position, target, step);

Here is the input for movement (only showed left x axis, because it's a big picture):

input

Here is the player 1 gameobject:

player1

And here is the player 2 gameobject:

player2

Do you see where the problem might be? My controller seems to work fine.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by pacific00 · Aug 27, 2014 at 06:43 AM

try putting the p1 script on an empty game object and instead of saying transform.position = Vector3.MoveTowards(transform.position, target, step);

cache the two players and move whichever u fell like moving..

here both players move coz , i guess u r calling this code in the update and both updates would get called..

Comment
Add comment · Show 1 · 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 EClancy · Aug 27, 2014 at 09:04 PM 0
Share

I'm not sure I understand this, you said "ins$$anonymous$$d of saying ..." but I'm not sure what you told me to do ins$$anonymous$$d.

All 4 of my player's updates get "called", but only the 2nd player moves with player 1. From your explanation I still don't understand why my problem happens.

Also, the chatspeak is a bit hard to understand.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Check if trigger collider is touching other trigger collider 2 Answers

Acces to a var from another Script. 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