Hello there,
I am making a game on mobile that uses the standard assets CrossPlatformManager scripts to use touch buttons for my character. I have gotten the character to move left, right and jump…but he doesnt jump on touch 100% of the time. He jumps while running with normal gravity with no problem, but when i rotate the gravity and character 90 degrees clockwise, he can no longer jump while moving left or right (relative to the gravity). He either has to stop moving to jump or run into an object to stop him from moving that direction to jump.
Technically, the player still moves and jumps, but after the gravity and player rotation, it makes controlling the player much more difficult because he no longer moves as he did before the rotation which breaks the controls essentially.
[SerializeField] float dirX; //float that determines if player is moving left or right
[SerializeField] float dirY; //float that determines if player is moving left or right (when gravity is rotated)
public float moveSpeed = 7f, jumpForce = 950f; //sets up player's speed and how high he jumps
Rigidbody2D rb; //reference to rigidbody
Animator anim; //reference to animator
[SerializeField] LayerMask whatIsGround; //sets up layermask for determining what is the ground (helps player jump)
[SerializeField] bool airControl = false; //whether or not the player can move in the air
bool facingRight = true; //For determining which way the player is currently facing.
bool grounded = false; //bool to turn off or on depending if player is grounded or not
Transform groundCheck; //A position marking where to check if the player is grounded.
Transform playerGraphics; //the transform of the player's graphics child
float groundedRadius = .2f; //sets up the radius of the ground checker
GravityRotationChecker gRC; //reference to the GravityRotationChecker script
public float enemyKnockback; //how far the player gets knocked back
public float knockbackCount; //counts how many times the player has been knockedback at any instance
public bool knockFromRight; //bool whether or not player was hit from right or not
public GameObject[] movementButtons; //set up array for the UI movement buttons
public GameObject[] vertMovementButtons; //set up array for the vertical UI movement buttons
// Use this for initialization
void Start()
{
//set up rigidbody, animator, player's graphics, groundcheck and GravityRotationChecker script
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
playerGraphics = transform.Find("Graphics");
groundCheck = transform.Find("GroundCheck");
gRC = GetComponent<GravityRotationChecker>();
if (playerGraphics == null)
{//if there are no playerGraphics, log the error
Debug.LogError("There are no graphics object as a child of the player!");
}
}
// Update is called once per frame
void Update()
{
if(gRC.GravityIsRotating)
{
DisableButtons();
EnableVerticalButtons();
}
//if the player is on the ground and airControl is true...
if (grounded || airControl)
{
if(gRC.JumpDirectionHasChanged)
{
anim.SetFloat("Speed", Mathf.Abs(dirY));
}
else
anim.SetFloat("Speed", Mathf.Abs(dirX));
//set the parameter "Speed" in the animator to the absolute value of the dirX variable
if (knockbackCount <=0) //if the knockback count is 0 or less...
{
//set the dirX variable to the Horizontal input (for the touch buttons)
dirX = CrossPlatformInputManager.GetAxis("Horizontal");
dirY = CrossPlatformInputManager.GetAxis("Vertical");
//if the player presses the jump button, is grounded, and the animator parameter bool is true...
if (CrossPlatformInputManager.GetButtonDown("Jump") && grounded && anim.GetBool("Ground"))
{
DoJump(); //jump
}
else if(CrossPlatformInputManager.GetButtonDown("RotatedJump") && grounded && anim.GetBool("Ground"))
{
DoRotatedJump(); //jump after gravity change
}
}
//if player hits an enemy or hazard, get knocked back
else
{
Knockback();
}
}
CheckForGraphicsFlip();
}
private void FixedUpdate()
{
if (gRC.JumpDirectionHasChanged)
{
MoveVertically();
}
else
MoveHorizontally();
//Moves player horizontally
//determines if player is touching the ground or not
grounded = Physics2D.OverlapCircle(groundCheck.position, groundedRadius, whatIsGround);
anim.SetBool("Ground", grounded); //set thes animator bool to equal what the grounded bool is
}
//function to make player jump
public void DoJump()
{
if (rb.velocity.y == 0)
{
rb.AddForce(new Vector2(0, jumpForce), ForceMode2D.Force);
}
}
//function to make player jump after gravity has rotated
public void DoRotatedJump()
{
if(rb.velocity.x == 0)
{
rb.AddForce(new Vector2(-jumpForce, 0), ForceMode2D.Force);
}
}
void MoveHorizontally()
{
//Moves the player left and right according to the buttons touched (left or right)
rb.velocity = new Vector2(dirX * moveSpeed, rb.velocity.y);
}
void MoveVertically()
{
rb.velocity = new Vector2(rb.velocity.x, dirY * moveSpeed);
}
//disables horizontal movement buttons
void DisableButtons()
{
foreach(GameObject b in movementButtons)
{
b.SetActive(false);
}
}
//enables vertical movement buttons
void EnableVerticalButtons()
{
foreach(GameObject b in vertMovementButtons)
{
b.SetActive(true);
}
}
Sorry for the stupid long class for movement. Let me know if i need to post the gravity rotation code. It is pretty standard but holler at me if needed.
Any help or ideas is appreciated.