Created
November 18, 2018 13:06
-
-
Save dinukapj/f08eb44b55ac5c0546c75f9eb4e759ae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.UI; | |
public class PlayerMovement : MonoBehaviour | |
{ | |
//SWIPE CONTROLS | |
private Animator anim; | |
//PC CONTROLS | |
public int speed = 6; | |
public int jumpForce = 300; | |
private float moveX; | |
private bool isGrounded = true; | |
private bool facingRight; | |
private bool movingEnabled = true; | |
private bool isClimbing; | |
private bool isHanging; | |
private int timeInAir = 0; //tracks how long it has been since the player has touched ground | |
private bool jumpedButtonClicked; | |
private bool interactButtonClicked; | |
private bool isNearHidingSpot; | |
private bool isHiding; | |
//HIDING | |
private GameObject nearestHidingSpot; | |
private Vector3 positionBeforeHiding; | |
//JOYSTICK | |
public VirtualJoystick joystick; | |
private void Start() | |
{ | |
anim = GetComponent<Animator>(); | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
//if (!isGrounded) | |
//{ | |
// timeInAir++; | |
//} | |
//else | |
//{ | |
// timeInAir = 0; | |
//} | |
PlayerMove(); | |
} | |
private void PlayerMove() | |
{ | |
//CONTROLS | |
if (Input.GetButtonDown("Jump") || jumpedButtonClicked) | |
{ | |
jumpedButtonClicked = false; | |
//jumping up when grounded | |
if (!isHanging && isGrounded) | |
{ | |
//stop running animation | |
anim.SetBool("isRunning", false); | |
//start jump animation | |
anim.SetBool("isJumping", true); | |
Jump(); | |
} | |
else if (isHanging) | |
{ | |
//if player is hanging, climb up | |
ClimbUp(); | |
} | |
} | |
if (Input.GetButtonDown("Interact") || interactButtonClicked) | |
{ | |
interactButtonClicked = false; | |
if (isNearHidingSpot) | |
{ | |
if (!isHiding) | |
{ | |
isHiding = true; | |
positionBeforeHiding = transform.position; | |
transform.position = nearestHidingSpot.GetComponent<HidingSpot>().hidingPosition.position; | |
} | |
else | |
{ | |
isHiding = false; | |
transform.position = positionBeforeHiding; | |
} | |
} | |
} | |
//Tracking Falling state | |
//if (timeInAir > 50) | |
//{ | |
// anim.SetBool("isFalling", true); | |
//} | |
//else | |
//{ | |
// anim.SetBool("isFalling", false); | |
//} | |
if (movingEnabled) | |
{ | |
moveX = Input.GetAxis("Horizontal"); | |
if (joystick.InputDirection.x != 0) | |
{ | |
moveX = joystick.InputDirection.x; | |
} | |
//direction | |
if (moveX < 0.0f) | |
{ | |
transform.eulerAngles = new Vector3(0, -90, 0); | |
facingRight = true; | |
anim.SetBool("isRunning", true); | |
} | |
else if (moveX > 0.0f) | |
{ | |
transform.eulerAngles = new Vector3(0, 90, 0); | |
facingRight = false; | |
anim.SetBool("isRunning", true); | |
} | |
//idle | |
if (moveX == 0.0f) | |
{ | |
anim.SetBool("isRunning", false); | |
} | |
//PHYSICS | |
gameObject.GetComponent<Rigidbody>().velocity = new Vector3(moveX * speed, gameObject.GetComponent<Rigidbody>().velocity.y); | |
} | |
} | |
private void ClimbUp() | |
{ | |
anim.SetBool("isHanging", false); | |
transform.position = lastCollidedPlatform.GetComponent<Platform>().climbPosition.transform.position; | |
gameObject.GetComponent<Rigidbody>().isKinematic = false; | |
movingEnabled = true; | |
isHanging = false; | |
} | |
public void Jump() | |
{ | |
isGrounded = false; | |
GetComponent<Rigidbody>().AddForce(Vector3.up * jumpForce); | |
} | |
private void OnCollisionEnter(Collision collision) | |
{ | |
if (collision.gameObject.tag == "Platform") | |
{ | |
isGrounded = true; | |
anim.SetBool("isJumping", false); | |
} | |
} | |
Collider lastCollidedPlatform; | |
private void OnTriggerEnter(Collider trigger) | |
{ | |
if (trigger.gameObject.tag == "Platform") | |
{ | |
//START HANGING | |
lastCollidedPlatform = trigger; | |
transform.position = lastCollidedPlatform.GetComponent<Platform>().climbPath.transform.position; | |
gameObject.GetComponent<Rigidbody>().isKinematic = true; | |
isHanging = true; | |
movingEnabled = false; | |
anim.SetBool("isHanging", true); | |
//Invoke("StopClimbAnimation", 1); | |
} | |
if (trigger.gameObject.tag == "Hiding Spot") | |
{ | |
isNearHidingSpot = true; | |
nearestHidingSpot = trigger.gameObject; | |
} | |
} | |
private void OnTriggerExit(Collider trigger) | |
{ | |
if (trigger.gameObject.tag == "Hiding Spot") | |
{ | |
isNearHidingSpot = false; | |
nearestHidingSpot = null; | |
} | |
} | |
//Deprecated | |
void StopClimbAnimation() | |
{ | |
anim.SetBool("isClimbing", false); | |
transform.position = lastCollidedPlatform.GetComponent<Platform>().climbPosition.transform.position; | |
gameObject.GetComponent<Rigidbody>().isKinematic = false; | |
movingEnabled = true; | |
} | |
public void Interact() | |
{ | |
} | |
public void InteractButtonClicked() | |
{ | |
interactButtonClicked = true; | |
} | |
public void JumpButtonClicked() | |
{ | |
jumpedButtonClicked = true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment