유니티 공부 4강... 프로그래밍 기초. 먼지청소기 만들어보기
unity 유니티 공부 2024. 10. 21. 15:54 |
ide 라고 불리는 스크립트 편집기 visual studio 설치 (20gb 용량필요)
개인을 위한 community 버전 체크 on
유니버설 어쩌구 체크 on , unity 체크 on
안되면 비쥬얼스크립트 ㅡ 솔루션 탐색기 ㅡ 우클릭 다시로드
+
로봇청소기 프로그래밍 해보기~
플레이어 될 로봇청소기 모델을 드래그해서 맵에 배치
이 로봇 청소기 이름을 Player 라고 명명
스크립트 폴더에서 모노 비헤비어 스크립트 하나 생성
스크립트이름을 PlayerController
라고 이름변경한다
PlayerController 라는 파일을
플레이어모델 콤포넨트로 추가함
PlayerController 를 더블 클릭해 ide 를 통해 열음
using UnityEngine;
public class NewMonoBehaviourScript : MonoBehaviour
{
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
라고 되어있는 기존 코드를
↓ 아래 코드로 복붙해 변경한다
using UnityEngine;
// Controls player movement and rotation.
public class PlayerController : MonoBehaviour
{
public float speed = 5.0f; // Set player's movement speed.
public float rotationSpeed = 120.0f; // Set player's rotation speed.
private Rigidbody rb; // Reference to player's Rigidbody.
// Start is called before the first frame update
private void Start()
{
rb = GetComponent<Rigidbody>(); // Access player's Rigidbody.
}
// Update is called once per frame
void Update()
{
}
// Handle physics-based movement and rotation.
private void FixedUpdate()
{
// Move player based on vertical input.
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = transform.forward * moveVertical * speed * Time.fixedDeltaTime;
rb.MovePosition(rb.position + movement);
// Rotate player based on horizontal input.
float turn = Input.GetAxis("Horizontal") * rotationSpeed * Time.fixedDeltaTime;
Quaternion turnRotation = Quaternion.Euler(0f, turn, 0f);
rb.MoveRotation(rb.rotation * turnRotation);
}
}
왼쪽위 하이어라키 메뉴에서, 메인 카메라를 드래그해서
Player 아래에 둬서 자식으로 설정
카메라를 적당한 위치로 조정, 지금 여기선 백뷰에서 살짝 y 축 위로 오게 설정함
Player 의 속성값인 속도와 회전속도를
플레이 하는 도중에 요리조리 조절해보고 적절한 값을 찾기
+
먼지 오브젝트를 맵에 배치
MonoBehaviour Script 하나 생성후
Collectible 로 이름짓기 . 이걸 먼지 오브젝트 콤포넌트로 적용
transform.Rotate(0, 0.5f ,0)
소수점 입력했는데 오류나면 0.5f
라고 f 를 넣어줄 것
using UnityEngine;
public class Collectible : MonoBehaviour
{
public float rotationSpeed;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Rotate(0, rotationSpeed, 0);
}
}
이렇게 public float 지정하면 에디터 창에서 편하게 값을 입력 가능
collectible 맨 아래에 온트리거 , 디스트로이 넣기
이렇게만 하면
dirt 오브젝트에 player 가 닿으면 dirt가 사라지게 만든거임
+
파티클 꾸미개
파티클 효과는 더블클릭해서 독립해서 본후
restart 버튼으로 여러번 돌려볼 수 있다
public GameObject onCollectEffect;
Collectible.cs 에 저 윗줄하나 추가후
원하는 파티클 드래그 해서 설정
// instantiate the particle effect
Instantiate(onCollectEffect, transform.position, transform.rotation);
끝줄에 이거 추가
파티클 터지는게
플레이어에게만 활성화 시키려면
플레이어에게 Player 태그를 걸어준 후
using UnityEngine;
public class Collectible : MonoBehaviour
{
public float rotationSpeed;
// Start is called once before the first execution of Update after the MonoBehaviour is created
public GameObject onCollectEffect;
// 이거 한 줄 추가하면 닿을 시 이펙트 생성 가능케해줌
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Rotate(0, rotationSpeed, 0);
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
// Destroy the collectible
Destroy(gameObject);
// instantiate the particle effect
Instantiate(onCollectEffect, transform.position, transform.rotation);
}
}
}
맨 마지막줄 if other compareTage Player 이거 적용 시키면 됨
다 되면 이런모습임
강의 출처
+
비쥬얼 스튜디오 가로 드래그 불편하면
모든언어 일반 - 자동 래핑, word wrap 기능을 킴