출처 : https://www.youtube.com/watch?v=_U7qUUfRMYs
중력 조절
- Edit -> Project Settings -> Physics -> Gravity에서 조절 가능.
RigidBody 속성
오브젝트 이동시키기
- Drag를 높이면 공기저항으로 인해 떨어지는속도나 이동하는 속도가 서서히 느려짐
- velocity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
private Rigidbody myRigid;
private void Start()
{
myRigid = GetComponent<Rigidbody>();
}
void Update()
{
if(Input.GetKey(KeyCode.R))
{
myRigid.velocity = Vector3.forward;
}
}
}
오브젝트 회전시키기
- angular Drag를 높이면 회전저항으로 인해 물체의 회전속도가 서서히 줄어듦.
- angularVelocity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
private Rigidbody myRigid;
private void Start()
{
myRigid = GetComponent<Rigidbody>();
}
void Update()
{
if(Input.GetKey(KeyCode.R))
{
myRigid.angularVelocity = Vector3.right;
}
}
}
- MoveRotation();
- 질량과 관성에 영향받지 않음.
- 물리효과 X
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
private Rigidbody myRigid;
private Vector3 rotation;
private void Start()
{
myRigid = GetComponent<Rigidbody>();
rotation = this.transform.eulerAngles;
}
void Update()
{
if(Input.GetKey(KeyCode.R))
{
rotation += new Vector3(90, 0, 0) * Time.deltaTime;
myRigid.MoveRotation(Quaternion.Euler(rotation));
}
}
}
- AddTorque()
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
private Rigidbody myRigid;
private void Start()
{
myRigid = GetComponent<Rigidbody>();
}
void Update()
{
if(Input.GetKey(KeyCode.R))
{
myRigid.AddTorque(Vector3.up);
}
}
}
오브젝트 회전속도 늘리기
- maxAngularVelocity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
private Rigidbody myRigid;
private void Start()
{
myRigid = GetComponent<Rigidbody>();
}
void Update()
{
if(Input.GetKey(KeyCode.R))
{
myRigid.maxAngularVelocity = 100; // 회전속도 최대치 증가
myRigid.angularVelocity = Vector3.right * 100;
}
}
}
오브젝트를 특정 위치로 한번에 이동시키기
- 물리효과 X
- 강제로 순간이동
- 관성과 질량에 영향을 받지 않음
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
private Rigidbody myRigid;
private void Start()
{
myRigid = GetComponent<Rigidbody>();
}
void Update()
{
if(Input.GetKey(KeyCode.R))
{
myRigid.MovePosition(new Vector3(2,2,2));
}
}
}
using UnityEngine;
public class CharacterMovementRigidbody : MonoBehaviour
{
public float moveSpeed = 5f;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 moveDirection = new Vector3(moveHorizontal, 0.0f, moveVertical).normalized;
rb.MovePosition(transform.position + moveDirection * moveSpeed * Time.fixedDeltaTime);
}
}
항목연속적인 이동 (CharacterMovementRigidbody)특정 위치로 순간 이동 (Test)
이동 방식 | 플레이어 입력에 따라 연속적인 이동 | 특정 키 입력 시 즉시 특정 위치로 이동 |
물리 기반 | 물리 법칙에 따라 이동 | 물리 법칙을 무시하고 강제로 이동 |
메서드 호출 위치 | FixedUpdate에서 호출 | Update에서 호출 |
관성 및 질량의 영향 | 관성과 질량에 영향을 받음 | 관성과 질량에 영향을 받지 않음 |
주 사용 사례 | 캐릭터와 같은 움직이는 오브젝트 | 순간 이동이나 텔레포트와 같은 경우 |
- AddForce는 Rigidbody에 물리적 힘을 가하여 이동시키는 방법입니다.
- 물리 엔진의 영향을 받아 더 자연스러운 이동을 만들어 줍니다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
private Rigidbody myRigid;
private void Start()
{
myRigid = GetComponent<Rigidbody>();
}
void Update()
{
if(Input.GetKey(KeyCode.R))
{
myRigid.AddForce(Vector3.forward);
}
}
}
물리적 상호작용이 중요한 경우 AddForce를, 특정 위치로 정확하게 이동시켜야 하는 경우 MovePosition을 사용하는 것이 좋습니다.
- AddExplosionForce(폭발세기, 폭발위치, 폭발반경)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
private Rigidbody myRigid;
private void Start()
{
myRigid = GetComponent<Rigidbody>();
}
void Update()
{
if (Input.GetKey(KeyCode.R))
{
myRigid.AddExplosionForce(20, this.transform.position, 20);
myRigid.AddExplosionForce(20, this.transform.right, 20);
}
}
}
카메라(Camera) (0) | 2024.06.25 |
---|---|
MeshRenderer, 마우스 클릭시 오브젝트 색 바꾸기 (0) | 2024.06.25 |
박스 콜라이더, 레이캐스트, OnTriggerEnter, OnCollisionEnter (0) | 2024.06.25 |
Transform 오브젝트 이동하기, 회전하기, 크기조절, 바라보기, 공전하기 (0) | 2024.06.22 |
스크립트의 직렬화(SerializeField / Serializable) (0) | 2024.05.30 |