[Unity] ๋ฌผ์ฒด ์›€์ง์ด๊ธฐ
๐ŸŽฎ Unity

[Unity] ๋ฌผ์ฒด ์›€์ง์ด๊ธฐ

โ€ป 2D์— 3D ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋„ฃ๊ฑฐ๋‚˜ 3D์— 2D ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋„ฃ์œผ๋ฉด ์ž‘๋™์ด ์•ˆ๋œ๋‹ค.

 

1. ์†๋ ฅ์„ ๋ฐ”๊พธ์–ด ๋ฌผ์ฒด ์›€์ง์ด๊ธฐ

์†๋ ฅ์— ์˜ํ•ด ๋ฌผ์ฒด๊ฐ€ ์›€์ง์ž„

    Rigidbody rigid;

    void Start()
    {
        rigid = GetComponent<Rigidbody>();    
        rigid.velocity = Vector3.right; // ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์†๋ ฅ์„ ๋ฐ›์Œ
    }
public class MyBall : MonoBehaviour
{
    Rigidbody rigid;

    void Start()
    {
        rigid = GetComponent<Rigidbody>();    
        
    }

  
    void FixedUpdate()
    {
        rigid.velocity = new Vector3(2, 4, 3); // ๋ฒกํ„ฐ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์†๋ ฅ ์ง€์ • ๊ฐ€๋Šฅ
    }
}

- velocity: ํ˜„์žฌ ์ด๋™ ์†๋„

 

โ€ป RigidBody ๊ด€๋ จ ์ฝ”๋“œ๋Š” FixedUpdate()์— ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•œ๋‹ค.

 

2. ํž˜์„ ๊ฐ€ํ•ด์„œ ๋ฌผ์ฒด ์›€์ง์ด๊ธฐ

space(Jump ๋ฒ„ํŠผ)์„ ๋ˆ„๋ฅด๋ฉด ์œ„์ชฝ์„ ํ–ฅํ•ด ์ ํ”„ํ•œ๋‹ค.

public class MyBall : MonoBehaviour
{
    Rigidbody rigid;

    void Start()
    {
        rigid = GetComponent<Rigidbody>();    
    }

    void FixedUpdate()
    {
   
        if(Input.GetButtonDown("Jump")){
            rigid.AddForce(Vector3.up * 50,ForceMode.Impulse); // ์บ๋ฆญํ„ฐ ์ ํ”„
        }
    }
}

- AddForce(Vec): Vec์˜ ๋ฐฉํ–ฅ๊ณผ ํฌ๊ธฐ๋กœ ํž˜์„ ์คŒ.

- ForceMode: ํž˜์„ ์ฃผ๋Š” ๋ฐฉ์‹ (๊ฐ€์†, ๋ฌด๊ฒŒ ๋ฐ˜์˜)

 

โ€ป Rigidbody > Mass์˜ ์˜ํ–ฅ์„ ๋ฐ›์Œ

โ€ป Jump ๋ฒ„ํŠผ์„ ๋ฌด์—‡์œผ๋กœ ์„ค์ •ํ•ด๋†“์•˜๋Š”์ง€ ๋ชจ๋ฅผ ๋•Œ, Edit(ํŽธ์ง‘) - ProjectSettings(ํ”„๋กœ์ ํŠธ ์„ค์ •) - Input Manager - Jump ์‹œ ํ™•์ธ ๊ฐ€๋Šฅ