유니티의 오브젝트 크기, 위치, 색 설정하기

|


오브젝트의 크기는 localScale 속성으로 설정합니다.

오브젝트의 색상은(Red, Green, Blue, Alpha)로 설정하며, 각각 0~1 사이의 값으로 지정합니다.(메트리얼이 존재해야됩니다.)

오브젝트의 위치는 position 속성으로 설정합니다.





Transform.localScale

Vector3 localScale;
Description

The scale of the transform relative to the parent.

using UnityEngine;
using System.Collections;

public class Example : MonoBehaviour {
    void Example() {
        transform.localScale += new Vector3(0.1F, 0, 0);
    }
}




Trackback 0 And Comment 0

유니티 WorldToScreenPoint를 사용하여 좌우 이동을 제한하는 스크립트

|

키보드로 오브젝트를 이동시 좌우나 앞으로의 이동을 화면 안으로 제한하고 싶을 경우 쓰는 스크립트





Camera.main.WorldToScreenPoint함수의
Camera.main의 의미는 태그가 mainCamera로 설정된 카메라를 의미합니다.
메인카메라를 기준으로 하는 월드 좌표를 스크린 좌표로 변행해주는 함수 입니다.
if문에서는 왼쪽으로 이동시(key < 0 ) 위치를 제한( pos.x > 0) 해주고 오른쪽으로 이동시(key > 0 ) 위치를 제한(pos.x < Screen.width)해주고 있습니다.

이와 같이 오브젝트의 위치를 스크린 좌표로 변환하면 화면 크기에 상관없게 오브젝트의 위치를 제한 할 수 있습니다.




Camera.WorldToScreenPoint


Vector3 WorldToScreenPoint(Vector3 position);
Description

Transforms position from world space into screen space.

Screenspace is defined in pixels. The bottom-left of the screen is (0,0); the right-top is (pixelWidth,pixelHeight). The z position is in world units from the camera.
using UnityEngine;
using System.Collections;

public class Example : MonoBehaviour {
    public Transform target;
    void Update() {
        Vector3 screenPos = camera.WorldToScreenPoint(target.position);
        print("target is " + screenPos.x + " pixels from the left");
    }
}


Trackback 0 And Comment 0

유니티 오브젝트 키보드로 움직이기

|
 
float amtMove = speed * Time.smoothDeltaTime; 는 오브젝트가 이동할시 프레임의 영향을 받지 않도록 일정한 속도로 움직이기 위해 설정된 값입니다. 

float key = Input.GetAxis("Horizontal");는 유니티 내의 Input관련 Horizontal로 정의된 값입니다. 키보드의 좌우 키를 누르거나 a d 키를 누를시 발동됩니다. 값은 -1~1사이 값입니다. 

transform.Translate(Vector3.right * amtMove * key , Space.World); 은 물체의 이동을 담당하는 함수입니다. Space.World는 월드 좌표 기준으로 이동한다는 의미입니다.



Trackback 0 And Comment 0