Files
PuzzleColorBall/Assets/Scripts/PlayerController.cs

87 lines
2.6 KiB
C#
Raw Normal View History

2022-12-01 09:03:46 +01:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public Rigidbody rb;
2023-02-02 10:58:27 +01:00
private CameraController cc;
2023-02-01 13:24:48 +01:00
//public float moveSpeed = 5f;
2022-12-01 09:03:46 +01:00
public float jumpforce = 5f;
2023-02-10 07:55:13 +01:00
private float sideMovement = 3f;
2022-12-01 09:03:46 +01:00
private Vector3 direction;
2023-02-09 13:39:52 +01:00
private float horizontal, vertical;
2023-02-10 07:55:13 +01:00
private bool isJumping;
2022-12-01 09:03:46 +01:00
private bool isOnGround;
2023-02-10 07:55:13 +01:00
//
public float holdTime = 0.3f;
private bool isTapped = false;
private float timeSinceLastTap = 0f;
2023-02-01 13:24:48 +01:00
//swipe movement
private Vector2 startTouchPosition;
private Vector2 endTouchPosition;
2022-12-01 09:03:46 +01:00
private void Awake() {
2023-02-02 10:58:27 +01:00
cc = FindObjectOfType<CameraController>();
2022-12-01 09:03:46 +01:00
}
2023-02-10 07:55:13 +01:00
private void Update(){
//jumping
2023-02-10 07:55:13 +01:00
if(Input.touchCount > 0){
Touch touch = Input.GetTouch(0);
if(touch.phase == TouchPhase.Began){
isTapped = true;
timeSinceLastTap = Time.time;
}
if(touch.phase == TouchPhase.Ended){
isTapped = false;
timeSinceLastTap = 0f;
}
if(isTapped && rb.transform.position.y <= 0.16f){
if(Time.time - timeSinceLastTap >= holdTime){
Debug.Log("Long tapped");
rb.AddForce(new Vector3(0, jumpforce, 0));
isTapped = false;
}
2023-02-09 13:39:52 +01:00
}
}
2022-12-01 09:03:46 +01:00
//new character controller with swipe lane changing
2023-02-01 13:24:48 +01:00
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began){
startTouchPosition = Input.GetTouch(0).position;
}
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended){
endTouchPosition = Input.GetTouch(0).position;
2023-02-10 07:55:13 +01:00
if(endTouchPosition.x < startTouchPosition.x){
2023-02-01 13:24:48 +01:00
//left
goLeft();
}else if(endTouchPosition.x > startTouchPosition.x){
//right
goRight();
}
}
2022-12-01 09:03:46 +01:00
}
2023-02-01 13:24:48 +01:00
private void goLeft(){
2023-02-10 07:55:13 +01:00
if(rb.transform.position.x >= 2.5f) return; //ne tudjon kimenni a savbol
2023-02-02 10:58:27 +01:00
cc.xPostion = -3;
2023-02-10 07:55:13 +01:00
//rb.transform.position = new Vector3(rb.transform.position.x + sideMovement, rb.transform.position.y, rb.transform.position.z);
rb.AddForce(new Vector3(300f, 0, 0));
2023-02-01 13:24:48 +01:00
}
private void goRight(){
2023-02-10 07:55:13 +01:00
if(rb.transform.position.x <= -2.5f) return; //ne tudjon kimenni a savbol
2023-02-02 10:58:27 +01:00
cc.xPostion = 3;
2023-02-10 07:55:13 +01:00
//rb.transform.position = new Vector3(rb.transform.position.x - sideMovement, rb.transform.position.y, rb.transform.position.z);
rb.AddForce(new Vector3(-300f, 0, 0));
2023-02-01 13:24:48 +01:00
}
2022-12-01 09:03:46 +01:00
}