½ºÅ©¸³Æ®¿¡¼ °´Ã¼ÀÇ À§Ä¡
À§Ä¡ ±¸Çϱâ : transform.position À§Ä¡ ¼³Á¤ : transform.position = new Vector3( x, y, z ) À§Ä¡ À̵¿ : transform.Translate( addX, addY, addZ ) ÇöÀçÀ§Ä¡¿¡¼ add ¸¸Å ¿òÁ÷ÀδÙ.
ƯÁ¤ ¹æÇâÀ¸·Î °´Ã¼ ȸÀü
transform.rotation = Quaternion.Euler(0, 0, 90)
z ÃàÀ» 90µµ ȸÀüÇÏ°í ÀÖ´Ù. ¶óµð¾ÈÀÌ ¾Æ´Ï¶ó µµ(degree)ÀÌ´Ù.
X, Y Æò¸é¿¡¼ Ç÷¹À̾î Á¦¾î
2D °ÔÀÓ¿¡¼ x, y Æò¸éÀ¸·Î Ç÷¹À̾ Á¦¾î ÇÒ¶§ÀÌ´Ù.
public class Car : MonoBehaviour {
void Update ()
{
float dir1 = Input.GetAxis("Horizontal");
transform.Translate( Vector3.right * dir1 * speed * Time.deltaTime);
float dir2 = Input.GetAxis("Vertical");
transform.Translate( Vector3.up * dir2 * speed * Time.deltaTime);
}
}
|
Ä«¸Þ¶ó ¹æÇâÀ¸·Î ÃÑ¾Ë ¹ß»ç
//Ä«¸Þ¶ó ¹æÇâÀ¸·Î ÃÑ¾Ë ¹ß»ç
public GameObject bulletPrefab;
private float vel = 20;
void Update ()
{
if(Input.GetButtonDown("Fire1"))
{
GameObject bullet = (GameObject)Instantiate(bulletPrefab, transform.position, transform.rotation);
Vector3 dir = transform.forward;
bullet.rigidbody.velocity = dir * vel;
}
}
|
¸¶¿ì½º Ŭ¸¯ À§Ä¡·Î ÃÑ¾Ë ¹ß»ç
//¸¶¿ì½º Ŭ¸¯ À§Ä¡·Î ÃÑ¾Ë ¹ß»ç
public GameObject bulletPrefab;
private float vel = 20;
void Update ()
{
if(Input.GetButtonDown("Fire1"))
{
GameObject bullet = (GameObject)Instantiate(bulletPrefab, transform.position, transform.rotation);
Vector3 screenPos = Input.mousePosition;
screenPos.z = 5.46f; //screenPos.z´Â Ä«¸Þ¶ó¿ÍÀÇ °Å¸®ÀÌ´Ù.
Vector3 worldPos = camera.ScreenToWorldPoint(screenPos);
Vector3 dir = (worldPos - transform.position).normalized;
bullet.rigidbody.velocity = dir * vel;
}
}
|
¸¶¿ì½º Ŭ¸¯½Ã ¸¶¿ì½º Ä¿¼ À§Ä¡·Î ÃѾËÀÌ ¹ß»ç µÈ´Ù. screenPos.z ÀÇ ºÎÁ¤È®¼º ¶§¹®ÀÎÁö´Â ¸ð¸£°ÚÀ¸³ª ÃѾ˰ú ¸¶¿ì½º Ä¿¼ÀÇ À§Ä¡°¡ Á¶±Ý¾¿ Â÷ÀÌ°¡ ³ª´Ù.
¿ÀºêÁ§Æ® ·ÎÄÃÃà À̵¿
public class LocalTransform : MonoBehaviour
{
public float moveSpeed = 1f;
public float rotateSpeed = 180f;
void Start () { }
void Update ()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
transform.position += transform.forward * v * moveSpeed * Time.deltaTime;
transform.rotation *= Quaternion.Euler(new Vector3(0f, h, 0f) * rotateSpeed * Time.deltaTime);
}
}
|
2D¿¡¼ ZÃàÀ¸·Î ȸÀü
2D¿¡¼ ³×¹æÇâÀ¸·Î À̵¿½Ã ¿ÀºêÁ¦Æ®¸¦ ȸÀüÇÑ´Ù.
¿ÀºêÁ§Æ® ±âº» ¹æÇâÀÌ ------> x = 1, y = 0 À¸·Î x, y °ª¿¡ µû¶ó °¢µµ´Â ´ÙÀ½°ú °°ÀÌ ³ª¿Â´Ù.
¿ÞÂÊÀ¸·Î À̵¿½Ã x = 1, y = 0 ( 0µµ ) À§ÂÊÀ¸·Î À̵¿½Ã x = 0, y = 1 ( 90µµ ) ¿À¸¥ÂÊÀ¸·Î À̵¿½Ã x = -1, y = 0 ( 180µµ ) ¾Æ·¡ÂÊÀ¸·Î À̵¿½Ã x = 0, y = -1 ( 270µµ )
float GetAngle( Vector3 a, Vector3 b )
{
float cosAngle = Mathf.Acos( Vector3.Dot( a, b ) ) / (Vector3.Magnitude(a) * Vector3.Magnitude(b));
cosAngle = Mathf.Rad2Deg * cosAngle;
//float angle = (a.x * b.y - a.y * b.x > 0.0f) ? cosAngle : -cosAngle;
float angle = (a.x * b.y - a.y * b.x > -0.000001f) ? cosAngle : 360 - cosAngle;
return angle;
}
void Rotate()
{
Vector3 vec1 = new Vector3( 1, 0 );
float degree = GetAngle( vec1, mDirection );
gameObject.transform.rotation = Quaternion.Euler(0, 0, degree);
}
|
ÂüÁ¶: ¿ÕÃʺ¸ µû¶óÇÏ¸ç °ÔÀÓ ¸¸µé±â(Ã¥) À¯´ÏƼ4 °ÔÀÓ °³¹ßÀÇ Á¤¼®(Ã¥)
|