Files
PuzzleColorBall/Assets/Scripts/PlayerController.cs

110 lines
4.1 KiB
C#
Raw Normal View History

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-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-03-07 21:06:25 +01:00
public ControllType activeControllType;
public Button leftButton;
public Button jumpButton;
public Button rightButton;
public enum ControllType
{
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-03-07 21:06:25 +01:00
activeControllType = ControllType.Button;
}
public void setControllType(ControllType controlltype){
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
if(activeControllType == ControllType.Swipe){
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;
}
if(isTapped && rb.transform.position.y <= 0.16f){
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-03-07 21:06:25 +01:00
}else if(activeControllType == ControllType.Button){
jumpforce = 2;
leftButton.onClick.AddListener(goRight);
jumpButton.onClick.AddListener(jump);
rightButton.onClick.AddListener(goLeft);
2023-02-01 13:24:48 +01:00
}
2022-12-01 09:03:46 +01:00
}
2023-02-16 14:55:48 +01:00
private void goLeft(){ //helyezze at a jatekos objektumot a balra levo savba
2023-02-10 07:55:13 +01:00
if(rb.transform.position.x >= 2.5f) return; //ne tudjon kimenni a savbol
2023-02-16 14:55:48 +01:00
cc.xPostion = -3; //kamera xPozicioja
rb.transform.position = new Vector3(rb.transform.position.x + sideMovement, rb.transform.position.y, rb.transform.position.z);
2023-02-01 13:24:48 +01:00
}
2023-02-16 14:55:48 +01:00
private void goRight(){ //helyezze at a jatekos objektumot a jobbra levo savba
2023-02-10 07:55:13 +01:00
if(rb.transform.position.x <= -2.5f) return; //ne tudjon kimenni a savbol
2023-02-16 14:55:48 +01:00
cc.xPostion = 3; //kamera xPozicioja
rb.transform.position = new Vector3(rb.transform.position.x - sideMovement, rb.transform.position.y, rb.transform.position.z);
2023-02-01 13:24:48 +01:00
}
2023-03-07 21:06:25 +01:00
private void jump(){
rb.AddForce(new Vector3(0, jumpforce, 0)); //ugras
}
2023-02-16 14:55:48 +01:00
}