Unity에서 2d 점프 시 자식 오브젝트들의 크기를 동일하게 조절하는 방법 (RectTransform 사용) > 자유게시판

본문 바로가기

사이트 내 전체검색

뒤로가기 자유게시판

Unity에서 2d 점프 시 자식 오브젝트들의 크기를 동일하게 조절하는 방법 (RectTransform 사용)

페이지 정보

작성자 최고관리자 작성일 24-06-19 11:17 조회 442 댓글 0

본문

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class jumptest : MonoBehaviour
{

    public RectTransform rectTransform; // UI 요소의 RectTransform
    public float jumpHeight = 200f; // 점프 높이
    public float jumpDuration = 10f; // 점프 지속 시간
    public float resizeMultiplier = 4f; // 크기 확대 비율

    public float minJumpTime = 0.5f; // 최소 점프 시간
    public float maxJumpTime = 5.0f; // 최대 점프 시간

    private Vector2 originalSize; // 원래 크기
    private Vector2 CurrentjumpingSize; // 점프 중인 상태의 크기  2단 ,3단 점프 때문에 (배터리만 있으면 계속 점프)
    public bool isJumping = false; // 점프 상태
    public float jumpTimer = 0f; // 점프 타이머
    private Rigidbody2D rb; // Rigidbody2D 컴포넌트

    public Vector2 originalScale; // 원래 크기
    public Vector2 targetScale; // 목표 크기
    public Vector2 currentScale; // 목표 크기

    void Start()
    {
        // 원래 크기를 저장
        //originalSize = rectTransform.sizeDelta;
        rb = GetComponent<Rigidbody2D>(); // Rigidbody2D 컴포넌트 가져오기

        // 원래 크기 저장
        originalScale = transform.localScale;

        // 목표 크기 설정
        targetScale = originalScale * resizeMultiplier;


        if (rb.mass < 10)  //중량이 너무 작으면 계속 떠 있는 문제 때문에
            rb.mass = 10;
        if (rb.mass > 100)  //중량이 너무 크면 100으로 고정
            rb.mass = 100;

    }

    void Update()
    {
        // 스페이스 키를 눌렀을 때 점프 시작
        if (Input.GetKeyDown(KeyCode.Space))
        {
            StartJump();
        }

        // 점프 중일 때 타이머 업데이트
        if (isJumping)
        {
            jumpTimer += Time.deltaTime;

            // 점프가 끝나면 원래 크기로 돌아옴
            if (jumpTimer >= jumpDuration)
            {
                EndJump();
            }
            else
            {
                // 중량에 따라 점프 높이 계산
                float jumpHeight = CalculateJumpHeight();

                // 절반 시간 동안 크기를 확대하고 나머지 절반 시간 동안 원래 크기로 돌아가기
                float t = jumpTimer / CalculateJumpTime();
                if (t <= 0.5f) // 절반 이하 시간
                {
                    t *= 2f; // t를 0~1 범위로 맞추기
                    //rectTransform.sizeDelta = Vector2.Lerp(CurrentjumpingSize, CurrentjumpingSize * resizeMultiplier, t);
                    transform.localScale = Vector2.Lerp(originalScale, originalScale * resizeMultiplier, t);
                   
                }
                else // 절반 이상 시간
                {
                    t = (t - 0.5f) * 2f; // t를 0~1 범위로 맞추기
                    //rectTransform.sizeDelta = Vector2.Lerp(CurrentjumpingSize * resizeMultiplier, CurrentjumpingSize, t);
                    transform.localScale = Vector2.Lerp(originalScale * resizeMultiplier, originalScale,  t);
                 
                }
            }
        }
    }

    void StartJump()
    {
        isJumping = true;
        jumpTimer = 0f;
    }

    void EndJump()
    {
        isJumping = false;

        // 크기를 원래대로 되돌림
        //rectTransform.sizeDelta = originalSize;
    }

    // 중량에 따라 다른 점프 시간 계산하는 함수
    float CalculateJumpTime()
    {


        float Jumptime = jumpDuration*10 / rb.mass;

        return Jumptime;
    }

    // 중량에 따라 다른 점프 높이 계산하는 함수
    float CalculateJumpHeight()
    {
        // 예시로 간단하게 중량에 따른 점프 높이 계산

        return jumpHeight / rb.mass;
    }
}

댓글목록 0

등록된 댓글이 없습니다.

회사소개 개인정보처리방침 서비스이용약관

사이트 정보

..

Copyright © magu.co.kr. All rights reserved.
PC 버전으로 보기