유니티 공부.. b 10 rigid body 외
unity 유니티 공부 2024. 11. 17. 06:48 |
코드의 흐름은
선언 -> 초기화 > 호출
생성 -> 초기작업 -> 호출
int number;
string message;
GameObject player;
생성 declaration
변수를 선언했지만 아직 값을 지니지 않은 상태
number = 10;
message = "Hello, World!";
player = new GameObject("Player");
초기작업 initialization
변수에 특정 값을 할당하는 것
사용될 준비가 됨
Debug.Log(message); // message 변수의 값을 출력
player.SetActive(true); // player 객체를 활성화
int result = AddNumbers(number, 5); // AddNumbers 메서드를 호출하여 결과를 result에 저장
호출 invocation , calling
메시지를 출력하거나
플레이어를 활성화하거나
넘버를 호출하여 결과에 저장함
+
기본 디폴트 유니티 스크립트 손보기
monobehaviour script 라고 검색하면 저 파일 나옴
저걸 보안 - 모든 권한에 체크
메모장으로 열기
// 주석 삭제
이러면 디폴트 값에서 주석을 없애 깨끗히 정리 가능해짐
+
velocity
현재 이동속도. 3디니까 Vector 3 값 사용
+
현재 2024 년 챗봇 ai copilot 에게
화면 캡쳐 후 유니티 오류 난 거 물어보면
해결법 알려줌
실제로 해보니 오류 해결됨
비쥬얼 스크립트에서 도구- 옵션- 텍스트 편집기- 모든 언어- codelens 를 해제 하면
"Unity 메시지 참조 0개"
이 문구를 제거 가능함.
지저분해서 해제
+
using UnityEngine;
public class RigidTest : MonoBehaviour
{
Rigidbody rigid;
void Start()
{
rigid = GetComponent<Rigidbody>();
rigid.linearVelocity = Vector3.right;
}
}
라고 적으면
물리에 의해 자동으로 굴러가나, x 의 오른쪽 방향으로 굴러간다
라는 뜻임
addForce
Vec 방향으로 힘을 줌
ForceMode (가속, 무게 반영)
힘을 주는 방식
+
rigidbody 관련된거 만약 보이드 업데이트에 쓰고 싶으면
FixedUpdate 에 하라고 함
+
using UnityEngine;
public class RigidTest : MonoBehaviour
{
Rigidbody rigid;
void Start()
{
rigid = GetComponent<Rigidbody>();
rigid.AddForce(Vector3.up * 1, ForceMode.Impulse);
}
}
Y축, 위로 통 하고 튀어오르는 건 Impulse 를 포함한 이런 코드를 쓴다 함
점프 할 때 쓰임.
rigid body 콤포넌트 의 mass 무게가 높을수록 저 스크립트 안 Vector3.up*1 이 값을 높여야 잘 튐
+
using UnityEngine;
public class RigidTest : MonoBehaviour
{
Rigidbody rigid;
void Start()
{
rigid = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
if (Input.GetButtonDown("Jump"))
{
rigid.AddForce(Vector3.up * 2, ForceMode.Impulse);
}
}
}
space 바 를 누르면 점프가 되게 구현한 예시
+
using UnityEngine;
public class RigidTest : MonoBehaviour
{
Rigidbody rigid;
void Start()
{
rigid = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
if (Input.GetButtonDown("Jump"))
{
rigid.AddForce(Vector3.up * 1, ForceMode.Impulse);
}
Vector3 vec = new Vector3
(
Input.GetAxisRaw("Horizontal"), //x 축
0,//y 축
Input.GetAxisRaw("Vertical") //z 축
);
rigid.AddForce(vec / 2, ForceMode.Impulse);
}
}
x 축 이동은 화살표 왼오
z 축 이동은 화살표 위아래
y 축 점프는 스페이스 바
이렇게 이동시키게 만들기
+
rigid.AddTorque 이러면 회전력을 주는 것이다
down 이라고 하면 오른쪽으로 돎
up 이라고 하면 왼쪽으로 돎
+