Skip to content
Permalink
e13c256101
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
252 lines (203 sloc) 7.82 KB
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Controller2D))]
public class Player : MonoBehaviour
{
//animation
public Animator animator; //here to better instantiate jumping
float moveSpeed = 10; //How Fast He moves
float gravity; //Gravity (No Rigid Body)
float maxJumpVelocity; //maxjump velocity
float minJumpVelocity; //min jump velocity
Vector3 velocity; //Velocity (Again since using raycasting need to create alll this)
float velocityXSmoothing;
//Wall Jumping
public float wallSlideSpeedMax = 4;//speed of sliding
public float wallStickTime = .15f;
float timeToWallUnstick;
public Vector2 wallJumpClimb; //Keep going up
public Vector2 wallJumpOff;//Jump off
public Vector2 wallLeap; //leap to other side
//Jumping
public float maxJumpHeight = 4; //height
public float minJumpHeight = 1;
public float timeToJumpApex = .4f; //time to get there
float accelerationTimeAirborne = .2f; //acceleration time
float accelerationTimeGrounded = .15f; //time to
Controller2D controller; //controller
Vector2 directionalInput;
public int currentHealth; //health
private int maxHealth = 100; //max health
public HealthBar healthbar; //Healthbar
bool crouch = false;
public GameObject door; //Public so I could drag door in
public GameObject door2; //WallJump Door
bool wallSliding;//need access to these in OnJump methods
int wallDirX;
void Start()
{
controller = GetComponent<Controller2D>(); //Get Components controller2d
gravity = -(2 * maxJumpHeight) / Mathf.Pow(timeToJumpApex, 2); //Derived from equation
maxJumpVelocity = Mathf.Abs(gravity) * timeToJumpApex; //Derived from equation
minJumpVelocity = Mathf.Sqrt(2 * Mathf.Abs(gravity) * minJumpHeight);
currentHealth = maxHealth; //Reset to max health
healthbar.SetMaxHealth(maxHealth); //Health bar to max
}
void Update()
{
///Call the methods here for neater code
CalculateVelocity();
HandleWallSliding();
//move
controller.Move(velocity * Time.deltaTime, directionalInput);
//if there is something above or below stop moving
if (controller.collisions.above || controller.collisions.below)
{
velocity.y = 0;
animator.SetBool("IsJumping", false);
}
}
//directional input method
public void SetDirectionalInput(Vector2 input)
{
directionalInput = input;
}
public void OnJumpInputDown()
{
if (wallSliding) //if you are sliding on a wall
{
if (wallDirX == directionalInput.x) //which direction
{
velocity.x = -wallDirX * wallJumpClimb.x;
velocity.y = wallJumpClimb.y; //jumping up the wall
animator.SetBool("IsJumping", true);
}
else if (directionalInput.x == 0)
{
velocity.x = -wallDirX * wallJumpOff.x;
velocity.y = wallJumpOff.y; //jumping off the wall
animator.SetBool("IsJumping", true);
}
else
{
velocity.x = -wallDirX * wallLeap.x;
velocity.y = wallLeap.y; //leaping from one side to other
animator.SetBool("IsJumping", true);
}
}
if (controller.collisions.below) //standing
{
velocity.y = maxJumpVelocity; //jump
animator.SetBool("IsJumping", true);
}
}
public void OnJumpInputUp() //jump up method
{
if (velocity.y > minJumpVelocity)
{
velocity.y = minJumpVelocity;
animator.SetBool("IsJumping", true);
}
}
void HandleWallSliding() //wall sliding method
{
wallDirX = (controller.collisions.left) ? -1 : 1; //left or right direction
wallSliding = false; //not on a wall
if ((controller.collisions.left || controller.collisions.right) && !controller.collisions.below && velocity.y < 0) //if you are on a wall
{
wallSliding = true; //bool to true
if (velocity.y < -wallSlideSpeedMax) //if velocity y is negative
{
velocity.y = -wallSlideSpeedMax; //set velocity y to wall slide speed, negative cuz you slide down
}
if (timeToWallUnstick > 0)
{
velocityXSmoothing = 0;//reset x velocity smoothing
velocity.x = 0;//reset x velocity smoothing
if (directionalInput.x != wallDirX && directionalInput.x != 0) //subtract time to unstick time cuz on wall
{
timeToWallUnstick -= Time.deltaTime;
}
else //thats how long to stick on wall
{
timeToWallUnstick = wallStickTime;
}
}
else
{
timeToWallUnstick = wallStickTime;
}
}
}
void CalculateVelocity() //calculate velocity method
{
float targetVelocityX = directionalInput.x * moveSpeed; //how fast we want to do
velocity.x = Mathf.SmoothDamp(velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below) ? accelerationTimeGrounded : accelerationTimeAirborne); //smoothing
velocity.y += gravity * Time.deltaTime; //velocity y
}
public GameObject dialogBox;//reference
public Text dialogText; //text
//Ontrigger Enter (AKA TAGS)
void OnTriggerEnter2D(Collider2D other) //Pickups
{
if (other.gameObject.CompareTag("Key")) //If you got the key
{
other.gameObject.SetActive(false); //key is not actice
door.SetActive(false); // Door is accessible
}
if (other.gameObject.CompareTag("Key2")) //If you got the key
{
other.gameObject.SetActive(false); //key is not actice
door2.SetActive(false); // Door is accessible
}
if (other.gameObject.CompareTag("Heart"))
{
other.gameObject.SetActive(false); // heart dissapears
currentHealth = maxHealth; // Full Health
healthbar.SetHealth(currentHealth); //Full HealthBar
}
if (other.gameObject.CompareTag("Enemy")) //take damage
{
TakeDamage(20);
}
///Sign tags, they turn off after a certain distance
if (other.gameObject.CompareTag("Sign0"))
{
dialogBox.SetActive(true);
dialogText.text = "Space to Jump\n" + "Arrows or WASD to move\n" + "E to attack\n" + "Esc to pause";
}
if (other.gameObject.CompareTag("Sign1"))
{
dialogBox.SetActive(true);
dialogText.text = "Q to throw Shurikens\n" + "Some Objects break..\n" + "Mysterious looking box..";
}
if (other.gameObject.CompareTag("Sign2"))
{
dialogBox.SetActive(true);
dialogText.text = "Go down the waterfall..\n" + "You will find all you need\n";
}
if (other.gameObject.CompareTag("Sign3"))
{
dialogBox.SetActive(true);
dialogText.text = "Hmmm....\n";
}
if (other.gameObject.CompareTag("SignOff")) //Turn off dialogue box
{
dialogBox.SetActive(false);
}
//Ninja star is in the weapon script (easier that way for some reason )
}
//Damage Function
void TakeDamage(int damage)
{
animator.SetTrigger("Hurt");
currentHealth -= damage; //take damage
healthbar.SetHealth(currentHealth); //modify health bar
if (currentHealth < 1)
{
Time.timeScale = 0f; //end frame
}
}
}