2022-12-01 09:03:46 +01:00
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
2023-03-07 21:06:25 +01:00
|
|
|
using UnityEngine.UI;
|
2022-12-01 09:03:46 +01:00
|
|
|
|
|
|
|
|
public class PlayerController : MonoBehaviour
|
|
|
|
|
{
|
2023-02-16 14:55:48 +01:00
|
|
|
public Rigidbody rb; //jatekos teste
|
2023-02-02 10:58:27 +01:00
|
|
|
private CameraController cc;
|
2023-05-13 15:09:38 +02:00
|
|
|
private IsGrounded isGrounded;
|
|
|
|
|
|
2023-02-16 14:55:48 +01:00
|
|
|
public float jumpforce = 5f; //mekkorat tudjon ugorni
|
|
|
|
|
private float sideMovement = 3f; //oldalra mennyit mozogjon
|
|
|
|
|
private Vector3 direction; //jatkos pozicio
|
|
|
|
|
private bool isJumping; //levegobe van e
|
|
|
|
|
public float holdTime = 0.3f; //meddig kell nyomni egy érintéshez
|
|
|
|
|
private bool isTapped = false; //kattintas erzekeles
|
|
|
|
|
private float timeSinceLastTap = 0f; //mennyi ido telt el a legutolso erintes ota
|
|
|
|
|
private Vector2 startTouchPosition; //erintes kezdo pozicio
|
|
|
|
|
private Vector2 endTouchPosition; //erintes vegpozicio
|
2023-02-01 13:24:48 +01:00
|
|
|
|
2023-05-13 15:09:38 +02:00
|
|
|
public ControlType activeControllType; //ezt kell atallitani hogy swipe-os vagy button-os legyen a mozgas
|
2023-03-07 21:06:25 +01:00
|
|
|
|
|
|
|
|
public Button leftButton;
|
|
|
|
|
public Button jumpButton;
|
|
|
|
|
public Button rightButton;
|
|
|
|
|
|
2023-05-13 15:09:38 +02:00
|
|
|
public enum ControlType
|
2023-03-07 21:06:25 +01:00
|
|
|
{
|
|
|
|
|
Swipe,
|
|
|
|
|
Button
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-01 09:03:46 +01:00
|
|
|
private void Awake() {
|
2023-02-16 14:55:48 +01:00
|
|
|
cc = FindObjectOfType<CameraController>(); //kamera vezerlo referencia
|
2023-05-13 15:09:38 +02:00
|
|
|
isGrounded = FindObjectOfType<IsGrounded>();
|
|
|
|
|
|
|
|
|
|
//activeControllType = ControlType.Button;
|
2023-03-07 21:06:25 +01:00
|
|
|
}
|
|
|
|
|
|
2023-05-13 15:09:38 +02:00
|
|
|
public void setControllType(ControlType controlltype){
|
2023-03-07 21:06:25 +01:00
|
|
|
activeControllType = controlltype;
|
2022-12-01 09:03:46 +01:00
|
|
|
}
|
|
|
|
|
|
2023-02-10 07:55:13 +01:00
|
|
|
private void Update(){
|
2023-03-07 21:06:25 +01:00
|
|
|
|
2023-05-13 15:09:38 +02:00
|
|
|
if(activeControllType == ControlType.Swipe){
|
2023-03-07 21:06:25 +01:00
|
|
|
leftButton.gameObject.SetActive(false);
|
|
|
|
|
jumpButton.gameObject.SetActive(false);
|
|
|
|
|
rightButton.gameObject.SetActive(false);
|
2023-02-10 07:55:13 +01:00
|
|
|
|
2023-03-07 21:06:25 +01:00
|
|
|
//jumping
|
|
|
|
|
if(Input.touchCount > 0){
|
|
|
|
|
Touch touch = Input.GetTouch(0); //elso erintes lekerese
|
2023-02-10 07:55:13 +01:00
|
|
|
|
2023-03-07 21:06:25 +01:00
|
|
|
if(touch.phase == TouchPhase.Began){ //ha az erintes elkezdotott
|
|
|
|
|
isTapped = true;
|
|
|
|
|
timeSinceLastTap = Time.time;
|
|
|
|
|
}
|
2023-02-10 07:55:13 +01:00
|
|
|
|
2023-03-07 21:06:25 +01:00
|
|
|
if(touch.phase == TouchPhase.Ended){ //ha az erintes befejezodott
|
2023-02-10 07:55:13 +01:00
|
|
|
isTapped = false;
|
2023-03-07 21:06:25 +01:00
|
|
|
timeSinceLastTap = 0f;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-13 15:09:38 +02:00
|
|
|
if(isTapped && isGrounded.isGrounded){
|
2023-03-07 21:06:25 +01:00
|
|
|
if(Time.time - timeSinceLastTap >= holdTime){ //ha nyomva tartotta a beallitott ideig
|
|
|
|
|
Debug.Log("Long tapped");
|
|
|
|
|
jump();
|
|
|
|
|
isTapped = false;
|
|
|
|
|
}
|
2023-02-10 07:55:13 +01:00
|
|
|
}
|
2023-02-09 13:39:52 +01:00
|
|
|
}
|
2022-12-01 09:03:46 +01:00
|
|
|
|
2023-03-07 21:06:25 +01:00
|
|
|
//new character controller with swipe lane changing
|
|
|
|
|
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began){ //elso erintes elkezdodott
|
|
|
|
|
startTouchPosition = Input.GetTouch(0).position;
|
|
|
|
|
}
|
|
|
|
|
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended){ //elso erintes befejezodott
|
|
|
|
|
endTouchPosition = Input.GetTouch(0).position;
|
2023-02-01 13:24:48 +01:00
|
|
|
|
2023-03-07 21:06:25 +01:00
|
|
|
if(endTouchPosition.x < startTouchPosition.x){ //balra huzott
|
|
|
|
|
//left
|
|
|
|
|
goLeft();
|
2023-02-01 13:24:48 +01:00
|
|
|
|
2023-03-07 21:06:25 +01:00
|
|
|
}else if(endTouchPosition.x > startTouchPosition.x){ //jobbra huzott
|
|
|
|
|
//right
|
|
|
|
|
goRight();
|
|
|
|
|
}
|
2023-02-01 13:24:48 +01:00
|
|
|
}
|
2023-05-13 15:09:38 +02:00
|
|
|
}else if(activeControllType == ControlType.Button){
|
2023-03-09 16:00:42 +01:00
|
|
|
//jumpforce = 100;
|
2023-03-09 14:07:44 +01:00
|
|
|
/*leftButton.onClick.AddListener(goRight);
|
2023-03-07 21:06:25 +01:00
|
|
|
jumpButton.onClick.AddListener(jump);
|
2023-03-09 14:07:44 +01:00
|
|
|
rightButton.onClick.AddListener(goLeft);*/
|
2023-02-01 13:24:48 +01:00
|
|
|
}
|
2022-12-01 09:03:46 +01:00
|
|
|
}
|
|
|
|
|
|
2023-03-09 14:07:44 +01:00
|
|
|
public void goLeft(){ //helyezze at a jatekos objektumot a balra levo savba
|
|
|
|
|
Debug.Log("kattintva bal");
|
|
|
|
|
if(rb.transform.position.x <= -2.5f) return; //ne tudjon kimenni a savbol
|
|
|
|
|
//cc.xPostion = -3; //kamera xPozicioja
|
|
|
|
|
//rb.transform.position = new Vector3(rb.transform.position.x - sideMovement, rb.transform.position.y, rb.transform.position.z);
|
|
|
|
|
|
|
|
|
|
if(rb.transform.position.x >= 2.5f){
|
|
|
|
|
rb.transform.position = new Vector3(0, rb.transform.position.y, rb.transform.position.z);
|
|
|
|
|
}else if(rb.transform.position.x >= -2.5f){
|
|
|
|
|
rb.transform.position = new Vector3(-3f, rb.transform.position.y, rb.transform.position.z);
|
|
|
|
|
}
|
2023-02-01 13:24:48 +01:00
|
|
|
}
|
|
|
|
|
|
2023-03-09 14:07:44 +01:00
|
|
|
public void goRight(){ //helyezze at a jatekos objektumot a jobbra levo savba
|
|
|
|
|
Debug.Log("kattintva jobb");
|
|
|
|
|
if(rb.transform.position.x >= 2.5f) return; //ne tudjon kimenni a savbol
|
|
|
|
|
//cc.xPostion = 3; //kamera xPozicioja
|
|
|
|
|
//rb.transform.position = new Vector3(rb.transform.position.x + sideMovement, rb.transform.position.y, rb.transform.position.z);
|
|
|
|
|
|
|
|
|
|
if(rb.transform.position.x <= -2.5f){
|
|
|
|
|
rb.transform.position = new Vector3(0, rb.transform.position.y, rb.transform.position.z);
|
|
|
|
|
}else if(rb.transform.position.x >= -2.5f){
|
|
|
|
|
rb.transform.position = new Vector3(3f, rb.transform.position.y, rb.transform.position.z);
|
|
|
|
|
}
|
2023-02-01 13:24:48 +01:00
|
|
|
}
|
2023-03-07 21:06:25 +01:00
|
|
|
|
2023-03-09 14:07:44 +01:00
|
|
|
public void jump(){
|
2023-05-13 15:09:38 +02:00
|
|
|
if(isGrounded.isGrounded){
|
2023-03-25 16:26:23 +01:00
|
|
|
rb.AddForce(new Vector3(0, jumpforce, 0)); //ugras
|
2023-05-13 15:09:38 +02:00
|
|
|
isGrounded.isGrounded = false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-09 14:07:44 +01:00
|
|
|
Debug.Log("jumped");
|
2023-03-07 21:06:25 +01:00
|
|
|
}
|
2023-05-13 15:09:38 +02:00
|
|
|
|
|
|
|
|
|
2023-02-16 14:55:48 +01:00
|
|
|
}
|