Professional Documents
Culture Documents
Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// make the player move left / right / jump / flips the player direction while
moving
/// </summary>
public class PlayerCtrl : MonoBehaviour
{
[Tooltip("This is an positive integer which speeds up the player movement")]
public int speedBoost; //set this to 5
public float jumpSpeed; //set this to 600
public bool isGrounded;
public Transform feet;
public float feetRadius;
public float boxWidth;
public float boxHeight;
public float delayForDoubleJump;
public LayerMask whatIsGround;
public Transform leftBulletSpawnPos, rightBulletSpawnPos;
public GameObject leftBullet, rightBullet;
public bool SFXOn;
public bool canFire;
public bool isJumping;
public bool isStuck;
Rigidbody2D rb;
SpriteRenderer sr;
Animator anim;
bool canDoubleJump;
public bool leftPressed, rightPressed;
void Start()
{
rb = GetComponent<Rigidbody2D>();
sr = GetComponent<SpriteRenderer>();
anim = GetComponent<Animator>();
}
}
void OnDrawGizmos()
{
Gizmos.DrawWireSphere(feet.position, feetRadius);
//Gizmos.DrawWireCube(feet.position, new Vector3(boxWidth, boxHeight, 0));
}
void MoveHorizontal(float playerSpeed)
{
rb.velocity = new Vector2(playerSpeed, rb.velocity.y);
if (playerSpeed < 0)
{
sr.flipX = true;
isJumping = false;
}
else if (playerSpeed > 0)
{
sr.flipX = false;
isJumping = false;
}
if(isJumping == false){
anim.SetInteger("State", 1);
}
if (rb.velocity.y < 0)
{
anim.SetBool("jumping", false);
anim.SetBool("falling", true);
if(isGrounded){
anim.SetBool("falling", false);
}
}
}
void Jump()
{
if (isGrounded)
{
isJumping = true;
rb.AddForce(new Vector2(0, jumpSpeed));
anim.SetBool("jumping", true);
Invoke("EnableDoubleJump", delayForDoubleJump);
}
canDoubleJump = false;
}
}
void EnableDoubleJump()
{
canDoubleJump = true;
}
void FireBullets()
{
if (canFire)
{
if (sr.flipX)
{
Instantiate(leftBullet, leftBulletSpawnPos.position,
Quaternion.identity);
}
if (!sr.flipX)
{
Instantiate(rightBullet, rightBulletSpawnPos.position,
Quaternion.identity);
}
}
}
SFXCtrl.instance.ShowCoinSparkle(other.gameObject.transform.position);
break;
case "Water":
// show the splash effect
SFXCtrl.instance.ShowSplash(other.gameObject.transform.position);
break;
case "Powerup_Bullet":
canFire = true;
Vector3 powerupPos = other.gameObject.transform.position;
Destroy(other.gameObject);
if (SFXOn)
SFXCtrl.instance.ShowBulletSparkle(powerupPos);
break;
default:
break;
}
}
}