- Home /
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):
Here is the player 1 gameobject:
And here is the player 2 gameobject:
Do you see where the problem might be? My controller seems to work fine.
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..
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
Follow this Question
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