Skip to content
Permalink
52d4c7a799
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
104 lines (69 sloc) 2.37 KB
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerController : Physicsobject
{
public float maxSpeed = 7;
public float jumpTakeOffSpeed = 7;
private SpriteRenderer spriteRenderer;
private Animator animator;
public GameObject fly;
public Transform jet;
public int fuel = 25;
public Text fuelText;
public bool facingRight = true;
// Use this for initialization
void Start()
{
fuel = 25;
SetfuelText();
}
void SetfuelText()
{
fuelText.text = "JETPAK: " + fuel.ToString(); //sets the coresponding text to the following
}
void Awake()
{
spriteRenderer = GetComponent<SpriteRenderer>(); //gets the sprite renderer
animator = GetComponent<Animator>(); //gets the animator
}
protected override void ComputeVelocity()
{
Vector2 move = Vector2.zero;
move.x = Input.GetAxis("Horizontal"); //gives the player the velocity needed to jump
if (Input.GetKeyDown("w")) //lets the player jump
{
if (fuel >= 1)//checks if there is fuel
{
velocity.y = jumpTakeOffSpeed;
Instantiate(fly, jet.position, jet.rotation);
fuel = fuel - 1;
fuelText.text = "JETPAK: " + fuel.ToString();
}
}
else if (Input.GetKeyUp("w")) //lets player cancel jump
{
if (velocity.y > 0)
{
velocity.y = velocity.y * 0.5f;
}
}
if (Input.GetKeyDown("s") && fuel <= 0)//refills fuel if at 0
{
fuel = fuel + 25;
fuelText.text = "JETPAK: " + fuel.ToString();
}
if (move.x < 0 && facingRight) Flip(); //flips the player right
if (move.x > 0 && !facingRight) Flip();//flips the player left
void Flip()
{
facingRight = !facingRight;
transform.Rotate(Vector2.up * 180);
//flips the player
}
animator.SetBool("grounded", grounded); //plays the jump animation in the animator
animator.SetFloat("velocityX", Mathf.Abs(velocity.x) / maxSpeed); //plays the running animation in the animator
targetVelocity = move * maxSpeed; //allows the player to move
}
}