본문 바로가기
유니티강좌

바운스볼 만들기, 벽 튕기기, 벽돌깨기 응용 (유니티 2D 기초강좌) - Unity & C# Script(Wall Bounce - Physics Material, AddForce)

by Ncube 2020. 12. 12.

 

 

 

볼이 벽에 부딪히면 튕겨져 나오는 방법을 배워봅시다. Physics Material 2D를 추가하여 바운딩 강도를 설정하고, 트레일 렌더러(Trail Renderer)를 추가하여 볼이 이동하는 궤적이 생기도록 합니다. 벽과 방해물에 부딪히면 바운딩 됩니다.

 

#바운스볼  #벽튕기기  #벽돌깨기

 

 

[WallBounce.cs]

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

public class WallBounce : MonoBehaviour
{
    [SerializeField] [Range(500f, 2000f)] float speed = 1000f;
    public Rigidbody2D rb;
    float randomX, randomY;

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

        randomX = Random.Range(-1f, 1f);
        randomY = Random.Range(-1f, 1f);

        Vector2 dir = new Vector2(randomX, randomY).normalized;

        rb.AddForce(dir * speed);
    }
}

 

 

[N-Cube채널 구독] www.youtube.com/channel/UCu48WEd7-leQbXoKiA_QM0w?sub_confirmation=1

 

엔큐브 스튜디오 [N Cube Studio]

© 2020 Google LLC CEO: 선다 피차이 주소: 1600 Amphitheatre Parkway, Mountain View, CA 94043, USA. 전화: 080-822-1450(무료)

www.youtube.com

 

댓글