Files
PuzzleColorBall/Assets/Scripts/PlayerController.cs

67 lines
2.0 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-02 10:58:27 +01:00
public float sideMovement = 3f;
2022-12-01 09:03:46 +01:00
private Vector3 direction;
private float horizontal, vertical, isJumping;
private bool isOnGround;
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-02 10:58:27 +01:00
void Update(){
//jumping
2023-02-01 13:24:48 +01:00
/*if (isJumping > 0 && isOnGround) {
2023-02-06 08:54:05 +01:00
rb.AddForce(new Vector3(0, jumpforce, 0));
2022-12-01 09:03:46 +01:00
isOnGround = false;
2023-02-01 13:24:48 +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-06 08:54:05 +01:00
if(endTouchPosition.x + 20< 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
}
private void OnCollisionEnter(Collision collision) {
if (collision.collider.CompareTag("Ground")) {
isOnGround = true;
}
}
2023-02-01 13:24:48 +01:00
private void goLeft(){
2023-02-02 10:58:27 +01:00
if(rb.transform.position.x == 3) return; //ne tudjon kimenni a savbol
cc.xPostion = -3;
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
}
private void goRight(){
2023-02-02 10:58:27 +01:00
if(rb.transform.position.x == -3) return; //ne tudjon kimenni a savbol
cc.xPostion = 3;
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
}
2022-12-01 09:03:46 +01:00
}