//using System.Collections; //using System.Collections.Generic; using UnityEngine; public class PlayerMovementController : MonoBehaviour { public float speed; float realSpeed; public TimeController tc; public float timeSpeed; public Rigidbody2D rb; public bool inStair = false; public float jumpForce = 2; public float stairsJumpSpeed = 50; public bool isGrounded; public bool isLeft = false; public GameObject wallCollisionDetector; public bool hit; public LayerMask DoorMask; public LayerMask WallMask; public LayerMask groundMask; public LayerMask stairsMask; public LayerMask bulletMask; public Material backgroundMaterial; public float OffsetSpeed; public float backGroundSpeed; public float wallDetectorRadius; public GameObject groundDetector; public bool inBullet; [Tooltip("1 is the grounded speed, more is slower and less is faster")] public float brakeJumpFactor = .5f; // Use this for initialization void Start () { timeSpeed = 1; rb = this.GetComponent(); } // Update is called once per frame private void FixedUpdate() { } void Update () { hit = Physics2D.OverlapCircle(wallCollisionDetector.transform.position, wallDetectorRadius, WallMask); hit = Physics2D.OverlapCircle(wallCollisionDetector.transform.position, wallDetectorRadius, DoorMask); isGrounded = Physics2D.OverlapCircle(groundDetector.transform.position, wallDetectorRadius, groundMask); inStair = Physics2D.OverlapCircle(groundDetector.transform.position, wallDetectorRadius, stairsMask); inBullet = Physics2D.OverlapCircle(groundDetector.transform.position, wallDetectorRadius, bulletMask); TimePower(); MoveRight(); MoveLeft(); Jump(); floatingEfectEraser(); if (inStair) { UpStairs(); DownStairs(); } } void TimePower() { if (Input.GetKey(KeyCode.Mouse1) || Input.GetKey("joystick button 5")) { timeSpeed = tc.timeSpeed; } else { timeSpeed = 1; } } void MoveRight() { if (Input.GetKey(KeyCode.D) || Input.GetAxisRaw("Horizontal") == 1) { realSpeed = speed * (1 / timeSpeed); transform.rotation = new Quaternion(0, 0, 0, 0); if (isGrounded) { transform.Translate((new Vector2(1, 0) * Time.deltaTime) * (speed * (1 / Time.timeScale))); } else { transform.Translate((new Vector2(1, 0) * Time.deltaTime) * (speed * (1 / brakeJumpFactor))); } isLeft = false; if (!hit) { backGroundSpeed = (backgroundMaterial.GetTextureOffset("_MainTex").x + -OffsetSpeed * Time.deltaTime); backgroundMaterial.SetTextureOffset("_MainTex", (new Vector2(1, 0) * backGroundSpeed)); } } } void MoveLeft() { if (Input.GetKey(KeyCode.A) || Input.GetAxisRaw("Horizontal") == -1) { realSpeed = speed * (1 / timeSpeed); transform.rotation = new Quaternion(0, -180, 0, 0); if (isGrounded) { transform.Translate((new Vector2(1, 0) * Time.deltaTime) * (speed * (1/ Time.timeScale))); } else { transform.Translate((new Vector2(1, 0) * Time.deltaTime) * ((speed * ( 1 / brakeJumpFactor)))); } isLeft = true; if (!hit) { backGroundSpeed = (backgroundMaterial.GetTextureOffset("_MainTex").x + OffsetSpeed * Time.deltaTime); backgroundMaterial.SetTextureOffset("_MainTex", (new Vector2(1, 0) * backGroundSpeed)); } } } void Jump() { if (Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown("joystick button 1")) { if (inStair) { transform.Translate((new Vector2(0, 1) * Time.deltaTime) * stairsJumpSpeed * 1 / Time.timeScale); } else if(isGrounded || inBullet) { if (Time.timeScale >= .6f) { rb.AddForce(new Vector2(0, 1) * Time.deltaTime * 100 * jumpForce, ForceMode2D.Impulse); } else { rb.AddForce(new Vector2(0, 1) * Time.deltaTime * 100 * jumpForce * (1 / Time.timeScale), ForceMode2D.Impulse); } } } } void UpStairs() { if (Input.GetKey(KeyCode.W) || Input.GetAxisRaw("Vertical") == -1) { transform.Translate((new Vector2(0, 1) * Time.deltaTime) * speed * 1 / Time.timeScale); } } void DownStairs() { if (Input.GetKey(KeyCode.S) || Input.GetAxisRaw("Vertical") == 1) { transform.Translate((new Vector2(0, -1) * Time.deltaTime) * speed * 1 / Time.timeScale); } } void floatingEfectEraser() { if (isGrounded) { rb.gravityScale = 1; }else if (isGrounded == false && inStair == false) { rb.gravityScale = 4; }else if (inStair) { rb.gravityScale = 0; } } void JumpPhysics() { if (inStair) { rb.velocity = new Vector2(0, 0); } else { rb.gravityScale = 1; } if (isGrounded) { //rb.gravityScale = 1; } } /*private void OnTriggerEnter2D(Collider2D collision) { if (collision.gameObject.tag == "Stairs") { rb.velocity = new Vector2(0,0); rb.gravityScale = 0; inStair = true; } } void OnTriggerExit2D(Collider2D collision) { if (collision.gameObject.tag == "Stairs") { rb.gravityScale = 1; inStair = false; } } private void OnCollisionStay2D(Collision2D collision) { if (collision.gameObject.tag == "Ground" || collision.gameObject.tag == "Bullet") { isGrounded = true; rb.gravityScale = 1; } } private void OnCollisionExit2D(Collision2D collision) { if(collision.gameObject.tag == "Ground") { isGrounded = false; } }*/ }