[#3-1. Script Review] 우주 정복 프로젝트

Maengkkong·2023년 11월 25일

MonsterBulletController

Middle Monster의 총알이 player를 향하도록 설정

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class MonsterBulletController : MonoBehaviour
{
    private Rigidbody2D bulletRigidbody;
    public float speed = 0.7f;
    public float lifeTime = 3f;


    // Start is called before the first frame update
    void Start()
    {
        bulletRigidbody = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        // 총알을 아래로 발사
        bulletRigidbody.AddForce(Vector2.down * speed, ForceMode2D.Impulse);
        Destroy(bulletRigidbody, lifeTime);
    }
}

GameObject.GetComponent<>();

GetComponent < ComponentType > ();
: 지정된 GameObject 유형의 구성 요소에 대한 참조를 가지고 온다.
: 매개변수 = Component의 구성 요소의 type
: 기본 반환값 = null

ex) myResults = otherGameObject.GetComponent< ComponentType >();


  • GameObject를 연결하여 유니티 내부 스크립트 class에서 코드를 작성할 경우

    : GameObject는 해당 연결된 GameObject의 구성 요소를 가져올 수 있습니다. 이 경우, GameObject를 생략하여도 같은 결과를 가져옵니다.

ex) myResults = GetComponent< ComponentType >()

  • GetComponent에서 호출한 GameObject는 하나만 연결되기 때문에 2개 이상 연결하려면 연결할 구성 요소에 대한 참조가 필요하다.
    : Public을 사용하여 GameObject 변수를 추가하여 유니티의 Inspector에서 해당 연결하고자 하는 객체를 끌어다가 연결합니다.

ex) public GameObject name;

GameObject.GetComponent


Rigidbody.AddForce();

Rigidbody에 힘을 추가하여 vector 방향을 따라 연속적으로 적용되며, 힘의 가속도, 충격량, 속도를 변경할 수 있습니다.

  • Rigidbody에만 적용될 수 있다.
  • GameObject가 비활성화된 경우 AddForce는 효과가 없다.

AddForce(Vector3 force, ForceMode mode = ForceMode.Force);
AddForce (float x , float y , float z , ForceMode 모드 = ForceMode.Force);
: force = global 좌표의 힘 (x, y, z)
: ForceMode = 적용할 힘의 유형

  • ForceMode.Force
    : 입력 힘 * DeltaTime / 질량 값으로 속도를 변경
    : 길이와 몸체와 질량의 질랴에 따라 다름
  • ForceMode.Acceleration
    : 매개변수를 가속도로 해석하여 힘 * DeltaTime값으로 속도를 변경
    : 길이에 따라 달라지지만 몸체의 질량에 의존하지 않음
  • ForeMode.Impulse
    : 매개변수를 충격량으로 해석하여 힘/질량 값에 따라 속도를 변경
    : 몸체의 질량에 따라 달라지지 않음
  • ForeMode.VelocityChange
    : 매개변수를 직접적인 속도 변경으로 해석하여 힘 값에 따라 속도를 변경
    : 몸체의 질량에 의존하지 않음

AddForce


Object.Destroy();

GameObject 구성 요소 또는 자산을 제거합니다.
Object가 Component이 경우 이 메서드는 GameObject에서 구성 요소를 제거하고 파괴합니다. GameObject인 경우 GameObject, 모든 구성 요소들, GameObject의 모든 변형 자식 또한 삭제됩니다.

Destroy(Object obj, float t = 0.0F);
: Obejct = 삭제할 객체
: time = 객체를 삭제하기 전에 지연할 수 있도록 설정(선택)

추가 ex) Destroy(GetComponent< BoxCollider >());
Destroy

0개의 댓글