반응형 Unity/소스코드6 카메라 Player 따라다니기 다른버젼 public class MainCameraController : MonoBehaviour { [SerializeField] Transform player; private void LateUpdate() { Vector3 targetPos = new Vector3(player.position.x, player.position.y, this.transform.position.z); transform.position = targetPos; } } 카메라가 따라갈 위치 변수를 만들어주고, 카메라 위치를 위치 변수로 설정하면 된다. 중요한 건 LateUpdate메서드를 사용해야 한다는 것이다. 플레이어가 움직이면 위치가 변경되는데 카메라 위치도 같은 프레임에 변경되게 하면 카메라 위치가 버벅된다. 플레이어 위치가.. 2023. 3. 15. 카메라가 플레이어 따라다니게 하기 cameraController 스크립트 생성 using System.Collections; using System.Collections.Generic; using UnityEngine; public class CameraController : MonoBehaviour { public float cameraSpeed = 3.0f; public GameObject player; private void Update() { Vector3 dir = player.transform.position - this.transform.position; Vector3 moveVector = new Vector3(dir.x * cameraSpeed * Time.deltaTime, dir.y * cameraSpeed * Tim.. 2023. 3. 15. 오브젝트 색상변경 스크립트 using UnityEngine; using System.Collections; public class ExampleBehaviourScript : MonoBehaviour { void Update() { if (Input.GetKeyDown(KeyCode.R)) { GetComponent ().material.color = Color.red; } if (Input.GetKeyDown(KeyCode.G)) { GetComponent().material.color = Color.green; } if (Input.GetKeyDown(KeyCode.B)) { GetComponent().material.color = Color.blue; } } } 2023. 3. 14. 객체 자동 이동 public class Enemy : MonoBehaviour { public Transform target; public Vector3 direction; public float velocity; public float default_velocity; public float accelaration; public Vector3 default_direction; void Start () { // 자동으로 움직일 방향 벡터 default_direction.x = Random.Range (-1.0f, 1.0f); default_direction.y = Random.Range (-1.0f, 1.0f); // 가속도 지정 (추후 힘과 질량, 거리 등 계산해서 수정할 것) accelaration = 0.1f; de.. 2023. 3. 6. ObjectItem.cs public class ObjectItem : MonoBehaviour, IObjectItem { [Header("아이템")] public Item item; [Header("아이템 이미지")] public SpriteRenderer itemImage; void Start() { itemImage.sprite = item.itemImage; } public Item ClickItem() { return this.item; } } 자료출처 : https://geojun.tistory.com/62 2023. 3. 6. 인벤토리 소스 item.cs using UnityEngine; [CreateAssetMenu] public class Item : ScriptableObject { public string itemName; public Sprite itemImage; } Slot.cs using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Slot : MonoBehaviour { [SerializeField] Image image; // Image Componet 담는곳 private Item _item; public Item item { get { return _item; } //슬롯의.. 2023. 3. 6. 이전 1 다음 반응형