Unity/Unity(๊ธฐ์ดˆ)

Unity ๋‚œ์ˆ˜ ์ƒ์„ฑ์€ Random.Range๋ฅผ ํ™œ์šฉํ•˜์ž

Rainbow๐ŸŒˆCoder 2021. 8. 17. 02:18
728x90

 

float rnd = Random.Range(0.0f, 0.5f);

0.0f~0.5f ์‚ฌ์ด์— ์ˆซ์ž๋ฅผ ๋žœ๋ค์œผ๋กœ ์ƒ์„ฑํ•˜์—ฌ ๋ณ€์ˆ˜ rnd์— ๋„ฃ์–ด์ฃผ๋Š” ์ฝ”๋“œ์ด๋‹ค.

 

 

<ํ™œ์šฉ์˜ˆ์‹œ>

์•„๋ž˜ ์†Œ์Šค์ฝ”๋“œ๋Š” A๋ฅผ ๋ˆ„๋ฅผ ์‹œ,

0.0~0.5 ์‚ฌ์ด์˜ ๋‚œ์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๊ฐ’์€ ๋ณ€์ˆ˜ rnd์— ์ €์žฅํ•˜๊ณ 

์˜ค๋ธŒ์ ํŠธ์˜ transform.position์— Vector3ํ˜• z๊ฐ’์„ ๋Œ€์ž…ํ•ด ์œ„์น˜๋ฅผ ๋ฐ”๊พธ๋Š” ์ฝ”๋“œ


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

public class studyScript : MonoBehaviour
{

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            float rnd = Random.Range(0.0f, 0.5f);
            this.transform.position = new Vector3(0.0f, 0.0f, rnd); //transform.position์€ ๊ฒŒ์ž„ ์˜ค๋ธŒ์ ํŠธ์˜ ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๊ณ  ์—ฌ๊ธฐ์—๋Š” Vector3ํ˜• ๊ฐ’์„ ๋Œ€์ž…ํ•  ์ˆ˜ ์žˆ๋‹ค.
        }

        if (Input.GetKeyDown(KeyCode.B))
        {
            float rnd = Random.Range(0.0f, 360.0f);
            this.transform.rotation = Quaternion.Euler(rnd, 0.0f, 0.0f);//transform.rotation์€ ๊ฒŒ์ž„ ์˜ค๋ธŒ์ ํŠธ์˜ ํšŒ์ „ ์ •๋„๋ฅผ ๋‚˜ํƒ€๋‚ด๊ณ  ์—ฌ๊ธฐ์—๋Š” Quaternion.Euler ๊ฐ’์„ ๋Œ€์ž…ํ•  ์ˆ˜ ์žˆ๋‹ค.
        }

        if (Input.GetKeyDown(KeyCode.C))
        {
            float rnd = Random.Range(1.0f, 1.5f);
            this.transform.localScale = new Vector3(rnd, rnd, rnd);//transform.localScale์€ ๊ฒŒ์ž„ ์˜ค๋ธŒ์ ํŠธ์˜ ํฌ๊ธฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๊ณ  ์—ฌ๊ธฐ์—๋Š” Vector3ํ˜• ๊ฐ’์„ ๋Œ€์ž…ํ•  ์ˆ˜ ์žˆ๋‹ค.
        }
    }
}

 

728x90