using UnityEngine;
using System.Collections;
public class NinjaMovement : MonoBehaviour {
Vector3 velocity = Vector3.zero;
public float jumpSpeed = 100f;
public float forwardSpeed = 1f;
public float minX = -1/9;
public GameObject gameOver;
bool didJump = false;
Animator animator;
public bool dead = false;
float deathCooldown;
public bool godMode = false;
// Use this for initialization
void Start () {
animator = transform.GetComponentInChildren<Animator>();
if(animator == null) {
Debug.LogError("Didn't find animator!");
}
}
// Do Graphic & Input updates here
void Update() {
if (Input.GetKeyDown (KeyCode.Space) || Input.GetMouseButtonDown (0)) {
Time.timeScale = 1;
}
if(dead) {
GameObject go = Instantiate(gameOver), new Vector3(0, 0, 0), Quaternation.Identity) as GameObject;
go.playerController = this;
Time.timeScale=0;
deathCooldown -= Time.deltaTime;
if(deathCooldown <= 0) {
if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0) ) {
Time.timeScale= 0;
Application.LoadLevel( Application.loadedLevel );
}
}
}
else {
if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0) ) {
didJump = true;
}
}
}
// Do physics engine updates here
void FixedUpdate () {
if(dead)
return;
rigidbody2D.AddForce( Vector2.right * forwardSpeed );
if(didJump) {
rigidbody2D.AddForce( Vector2.up * jumpSpeed );
animator.SetTrigger("DoJump");
didJump = false;
}
if(rigidbody2D.velocity.y > 0) {
transform.rotation = Quaternion.Euler(0, 0, 0);
}
else {
float angle = Mathf.Lerp (0, 45, (-rigidbody2D.velocity.y / 3f) );
transform.rotation = Quaternion.Euler(0, 0, angle);
}
}
void OnCollisionEnter2D(Collision2D collision) {
if(godMode)
return;
animator.SetTrigger("Death");
dead = true;
deathCooldown = 0.5f;
}
}