void SimulatePlayer(int index, PlayerInfo2D player, bool isInputAllowed, int dir) { SPCharCtr2d _controller = player.controller; // grab our current _velocity to use as a base for all calculations player.velocity = _controller.velocity; if (_controller.isGrounded) { player.velocity.y = 0; } if (isInputAllowed && dir == 1) { if (!disableControl) { normalizedHorizontalSpeed = 1; setSpriteDirection(player, -1); // if (_controller.isGrounded) { // player.animator.Play (Animator.StringToHash ("Run")); // } } } else if (isInputAllowed && dir == -1) { if (!disableControl) { normalizedHorizontalSpeed = -1; setSpriteDirection(player, 1); // if (_controller.isGrounded) { // player.animator.Play (Animator.StringToHash ("Run")); // } } } else { normalizedHorizontalSpeed = 0; // if (_controller.isGrounded) { // if (characterIndexUnderControl == index) { // player.animator.Play (Animator.StringToHash ("Idle_Active")); // } else { // player.animator.Play (Animator.StringToHash ("Idle")); // } // // } } // apply horizontal speed smoothing it var smoothedMovementFactor = _controller.isGrounded ? groundDamping : inAirDamping; // how fast do we change direction? player.velocity.x = Mathf.Lerp(player.velocity.x, normalizedHorizontalSpeed * runSpeed, Time.deltaTime * smoothedMovementFactor); // apply gravity before moving player.velocity.y += gravity * Time.deltaTime; _controller.move(player.velocity * Time.deltaTime); }
void SimulatePlayer(int index, PlayerInfo2D player, bool isInputAllowed) { CharacterController2D _controller = player.controller; Transform spriteTransform = player.spriteTransform; // grab our current _velocity to use as a base for all calculations player.velocity = _controller.velocity; if (_controller.isGrounded) { player.velocity.y = 0; } if (isInputAllowed && (InputManager.ActiveDevice.LeftStickX.Value < 0 || Input.GetKey(KeyCode.LeftArrow))) { if (_playerXYZ.disabledDir[index] != -1) { normalizedHorizontalSpeed = 1; if (spriteTransform.localScale.x < 0f) { spriteTransform.localScale = new Vector3(-spriteTransform.localScale.x, spriteTransform.localScale.y, spriteTransform.localScale.z); } if (_controller.isGrounded) { player.animator.Play(Animator.StringToHash("Run")); } } } else if (isInputAllowed && (InputManager.ActiveDevice.LeftStickX.Value > 0 || Input.GetKey(KeyCode.RightArrow))) { if (_playerXYZ.disabledDir[index] != 1) { normalizedHorizontalSpeed = -1; if (spriteTransform.localScale.x > 0f) { spriteTransform.localScale = new Vector3(-spriteTransform.localScale.x, spriteTransform.localScale.y, spriteTransform.localScale.z); } if (_controller.isGrounded) { player.animator.Play(Animator.StringToHash("Run")); } } } else { normalizedHorizontalSpeed = 0; if (_controller.isGrounded) { if (_characterIndexUnderControl == index) { if (Character3D.IsRotating) { player.animator.Play(Animator.StringToHash("rotating_active")); } else { player.animator.Play(Animator.StringToHash("Idle_Active")); } } else { if (Character3D.IsRotating) { player.animator.Play(Animator.StringToHash("rotating")); } else { player.animator.Play(Animator.StringToHash("Idle")); } } } } // we can only jump whilst grounded // if( isInputAllowed && _controller.isGrounded && (InputManager.ActiveDevice.Action1.WasPressed || Input.GetKeyDown(KeyCode.UpArrow)) ) // { // player.velocity.x=0; // player.velocity.y = Mathf.Sqrt( 2f * jumpHeight * -gravity ); // player.animator.Play( Animator.StringToHash( "Jump" ) ); // } // apply horizontal speed smoothing it var smoothedMovementFactor = _controller.isGrounded ? groundDamping : inAirDamping; // how fast do we change direction? player.velocity.x = Mathf.Lerp(player.velocity.x, normalizedHorizontalSpeed * runSpeed, Time.deltaTime * smoothedMovementFactor); // apply gravity before moving player.velocity.y += gravity * Time.deltaTime; _controller.move(player.velocity * Time.deltaTime); }
public void setSpriteDirection(PlayerInfo2D player, int direction) { player.spriteModel.rotation = Quaternion.Euler(0, 90 + 90 * direction, 0); }