- Home /
Question by
managerofSAW · Aug 13, 2015 at 09:02 AM ·
touchgravitychar
Can't use more than 1 button and falls slow
How would I make it so that you could run and jump? Also when falling and trying to use air control the character would not fall at their original pace. I'm new here so an explanation would be great.
using UnityEngine; using System.Collections; public class PlatformerCharacter2D : MonoBehaviour {
public float maxSpeed = 15f;
bool facingRight = true;
Animator anim;
//sets up the grounded stuff
bool grounded = false;
bool touchingWall = false;
public Transform groundCheck;
public Transform wallCheck;
float groundRadius = 0.2f;
float wallTouchRadius = 0.2f;
public LayerMask whatIsGround;
public LayerMask whatIsWall;
public float jumpForce = 500f;
public float jumpPushForce = 2f;
public GUITexture guiLeft;
public GUITexture guiRight;
public GUITexture guiJump;
bool moveLeft, moveRight, doJump = false;
public float moveSpeed = 5f;
float move = 0;
//double jump
bool doubleJump = false;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void FixedUpdate () {
if (moveLeft)
{
rigidbody2D.velocity = -Vector2.right * moveSpeed;
}
if (moveRight)
{
rigidbody2D.velocity = Vector2.right * moveSpeed;
}
// The player is grounded if a linecast to the groundcheck position hits anything on the ground layer.
grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
touchingWall = Physics2D.OverlapCircle(wallCheck.position, wallTouchRadius, whatIsWall);
anim.SetBool("Ground", grounded);
if (grounded)
{
doubleJump = false;
}
if (touchingWall)
{
grounded = false;
doubleJump = false;
}
anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
anim.SetFloat("Speed", Mathf.Abs (move));
rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);
// If the input is moving the player right and the player is facing left...
if(move > 0 &&!facingRight){
// ... flip the player.
Flip ();
}// Otherwise if the input is moving the player left and the player is facing right...
else if(move < 0 && facingRight){
// ... flip the player.
Flip ();
}
}
void Update()
{
if (Input.touchCount > 0)
{
// Get the touch info
Touch t = Input.GetTouch(0);
// Did the touch action just begin?
if (t.phase == TouchPhase.Began)
{
if (guiLeft.HitTest(t.position, Camera.main))
{
moveLeft = true;
move = -1;
if((grounded) && (guiJump.HitTest(t.position, Camera.main)))
{
anim.SetBool("Ground", false);
rigidbody2D.AddForce(new Vector2(0, jumpForce));
if(!grounded)
{
doubleJump = false;
}
}
if (touchingWall && (guiJump.HitTest(t.position, Camera.main)))
{
WallJump ();
}
}
// Are we touching the right arrow?
if (guiRight.HitTest(t.position, Camera.main))
{
moveRight = true;
move = 1;
if((grounded) && (guiJump.HitTest(t.position, Camera.main)))
{
anim.SetBool("Ground", false);
rigidbody2D.AddForce(new Vector2(0, jumpForce));
if(!grounded)
{
doubleJump = false;
}
}
if (touchingWall && (guiJump.HitTest(t.position, Camera.main)))
{
WallJump ();
}
}
// If the jump button is pressed and the player is grounded then the player should jump.
if((grounded) && (guiJump.HitTest(t.position, Camera.main)))
{
anim.SetBool("Ground", false);
rigidbody2D.AddForce(new Vector2(0, jumpForce));
if(!grounded)
{
doubleJump = false;
}
}
if (touchingWall && (guiJump.HitTest(t.position, Camera.main)))
{
WallJump ();
}
}
if (t.phase == TouchPhase.Ended)
{
Debug.Log("hajdhfjadsklj");
// Stop all movement
moveLeft = false;
moveRight = false;
rigidbody2D.velocity = Vector2.zero;
move = 0;
}
}
}
void WallJump ()
{
rigidbody2D.AddForce (new Vector2 (jumpPushForce, jumpForce));
}
void Flip()
{
// Switch the way the player is labelled as facing
facingRight = !facingRight;
//Multiply the player's x local cale by -1
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
Comment