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