-
[Roll-a-Ball] PlayerController 스크립트Unity(유니티) 2022. 4. 13. 12:43
player에 들어가는 스크립트
pickup타겟과 충돌 시 카운팅 되는 UI 로직도 포함하고 있음
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; using TMPro; public class PlayerController : MonoBehaviour { public float speed = 0; public TextMeshProUGUI countText; public GameObject winTextObject; private Rigidbody rb; private int count; private float movementX; private float movementY; // Start is called before the first frame update void Start() { rb = GetComponent<Rigidbody>(); count = 0; SetCountText(); winTextObject.SetActive(false); } //구체에서 움직임 입력데이터를 Vector2 변수에 저장 void OnMove(InputValue movementValue) { Vector2 movementVector = movementValue.Get<Vector2>(); movementX = movementVector.x; movementY = movementVector.y; } void SetCountText() { countText.text = "Count: " + count.ToString(); if(count >= 12) { winTextObject.SetActive(true); } } //프레임마다 힘을 추가함 private void FixedUpdate() { Vector3 movement = new Vector3(movementX, 0.0f, movementY); rb.AddForce(movement * speed); } //충돌 이벤트 private void OnTriggerEnter(Collider other) { if(other.gameObject.CompareTag("PickUp")) { other.gameObject.SetActive(false); count = count + 1; SetCountText(); } } }
'Unity(유니티)' 카테고리의 다른 글
[Roll-a-Ball] Rotator 큐브 회전 스크립트 (0) 2022.04.13 [Roll-a-Ball]CameraController 스크립트 (0) 2022.04.13 [UIUXSamples(2)] Password InputField (0) 2022.04.12 [UIUXSamples(2)] UIManager 스크립트 (0) 2022.04.12 [UIUXSamples(2)] Dropdown Color (0) 2022.04.12