본문 바로가기
Unity

볼 슈터, OnEnable()

by imagineer_jinny 2021. 7. 20.

ShooterPivot 자식으로 FirePos라는 빈 게임오브젝트를 만들고 위치를 Barrel 앞으로

 

BallShooter.cs 

변수설정

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

public class BallShooter : MonoBehaviour
{
    //ball 프리팹을 가져와서 찍어낼 것임,rigidbody로 찍어내면 바로 힘 갖다 쓸 수 있음
    public Rigidbody ball;

    //발사할 위치
    public Transform firePos;

    //슬라이더 UI value 수정
    public Slider powerSlider;

    //포탄 소리 재생 위한 AudioSource 카세트 플레이어 역할
    public AudioSource shootingAudio;

    //카세트 테이프 역할의 AudioClip
    public AudioClip fireClip; //발사할 때
    public AudioClip chargingClip; //장전할 때

    //발사 힘 (fireButton 누르자마자 힘)
    public float minForce = 15f;
    //최대 힘
    public float maxForce = 30f;

    //몇초만에 힘이 채워질지
    public float chargingTime = 0.75f;

    //현재 힘을 나타내주는 힘
    private float currentForce;

    //충전 속도(누르고있는동안 힘이 1초에 얼마나 충전될지)
    private float chargeSpeed;

    //이미 발사되었다면 또 다시 발사 못하도록 체크할 것
    private bool fired;

}

 

속도 = 거리 / 시간

 

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

public class BallShooter : MonoBehaviour
{
    //ball 프리팹을 가져와서 찍어낼 것임,rigidbody로 찍어내면 바로 힘 갖다 쓸 수 있음
    public Rigidbody ball;

    //발사할 위치
    public Transform firePos;

    //슬라이더 UI value 수정
    public Slider powerSlider;

    //포탄 소리 재생 위한 AudioSource 카세트 플레이어 역할
    public AudioSource shootingAudio;

    //카세트 테이프 역할의 AudioClip
    public AudioClip fireClip; //발사할 때
    public AudioClip chargingClip; //장전할 때

    //발사 힘 (fireButton 누르자마자 힘)
    public float minForce = 15f;
    //최대 힘
    public float maxForce = 30f;

    //몇초만에 힘이 채워질지
    public float chargingTime = 0.75f;

    //현재 힘을 나타내주는 힘
    private float currentForce;

    //충전 속도(누르고있는동안 힘이 1초에 얼마나 충전될지)
    private float chargeSpeed;

    //이미 발사되었다면 또 다시 발사 못하도록 체크할 것
    private bool fired;

    //컴포넌트가 꺼져있다가 켜지는 상태에서 매번 발동됨
    //Start는 한번만 OnEnable은 여러번
    //초기화 코드 넣어주기. 라운드 바뀔때마다 초기화 시켜야 하니까
    private void OnEnable()
    {
        currentForce = minForce;
        powerSlider.value = minForce;
        fired = false;
    }

    private void Start()
    {
        //누르고있는 동안 힘이 충전되는 속도
        chargeSpeed = (maxForce - minForce) / chargingTime;

    }

    private void Update()
    {

        if(fired==true)
        {
            return; //연속발사 안되게 막기
        }
        powerSlider.value = minForce;

        if(currentForce>=maxForce && !fired) //힘이충분히많아 발사처리해야하는경우
        {
            currentForce = maxForce;
            //발사처리
            Fire();
        }
        else if(Input.GetButtonDown("Fire1")) //발사버튼을 처음 누른 그 순간
        {
            fired = false; //연속발사하게하기
            currentForce = minForce;
            shootingAudio.clip = chargingClip;
            shootingAudio.Play();
        }
        else if(Input.GetButton("Fire1")&&!fired)//발사버튼누르고있는동안
        {
            currentForce = currentForce + chargeSpeed * Time.deltaTime;
            powerSlider.value = currentForce;
        }
        else if(Input.GetButtonUp("Fire1")&&!fired)//발사버튼에서 손 뗄때
        {
            //발사처리
            Fire();
        }

    }

    private void Fire()
    {
        fired = true;
        Rigidbody ballInstance= Instantiate(ball,firePos.position,firePos.rotation);
        //transform의 내장기능중 방향을 벡터로 반환하는게 있음 
        //forward는 앞 방향을 벡터로
        ballInstance.velocity = currentForce * firePos.forward;
        shootingAudio.clip = fireClip;
        shootingAudio.Play();

        currentForce = minForce;
    }
}

 

그 다음 Ball Shooter 체크박스 해제해줘야 함.

왜? 

Shooter Rotator가 자기가 세팅이 다 된 다음에 Ball Shooter을 켜주기 위해

즉 Shooter Rotator가 끝난 다음에 Ball Shooter가 동작하도록 만들기

 

Shooter Rotator 들어가서 새로운 변수 하나 추가해주기

댓글