RigidBody를 통한 유니티 물체 옮기기, 회전
예제
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
public class PhysicsControlObj : MonoBehaviour
{
public float _force = 10;
public float _speed = 3;
public float _rotSpeed = 5;
Rigidbody _rgbd3D;
private void Awake()
{
_rgbd3D = GetComponent<Rigidbody>();
}
private void Update()
{
float dirX = 0, dirZ = 0;
float rotY = 0;
if (Input.GetKey(KeyCode.UpArrow))
{
dirZ += 1;
}
if (Input.GetKey(KeyCode.DownArrow))
{
dirZ -= 1;
}
if (Input.GetKey(KeyCode.LeftArrow))
{
dirX -= 1;
}
if (Input.GetKey(KeyCode.RightArrow))
{
dirX += 1;
}
if (Input.GetKey(KeyCode.Delete))
{
rotY -= 1;
}
if (Input.GetKey(KeyCode.PageDown))
{
rotY += 1;
}
Vector3 vDir = new Vector3(dirX, 0, dirZ);
vDir.Normalize();
//_rgbd3D.AddForce(vDir * _force);
if (rotY != 0)
{
Quaternion target = _rgbd3D.rotation * Quaternion.Euler(0, _rotSpeed * rotY, 0);
_rgbd3D.MoveRotation(target);
}
_rgbd3D.MovePosition(_rgbd3D.position + _rgbd3D.rotation * vDir * _speed * Time.deltaTime);
}
}
Input 매니저와 transform 을 통한 이동
Input 매니저는 Edit > Project Setting 에서 확인 가능
(매니저에서 RotLR 추가)
예제
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DMovementControlObj : MonoBehaviour
{
public float _speed = 3;
public float _rotSpeed = 180;
void Update()
{
//float mz = Input.GetAxis("Vertical"); // -1 ~ 1
float mz = Input.GetAxis("Vertical"); // -1, 0, 1
float mx = Input.GetAxis("Horizontal"); // -1, 0, 1
float ry = Input.GetAxis("RotLR");
//Vector3 vDir = transform.rotation * new Vector3(mx, 0, mz);
//vDir = (vDir.magnitude > 1) ? vDir.normalized : vDir;
//transform.position += vDir * _speed * Time.deltaTime;
Vector3 vDir = new Vector3(mx, 0, mz);
transform.Translate(vDir * _speed * Time.deltaTime);
//if (ry != 0)
//{
// transform.rotation *= Quaternion.Euler(0, _rotSpeed * Time.deltaTime * ry, 0);
//}
transform.Rotate(Vector3.up * _rotSpeed * ry * Time.deltaTime);
}
}
픽킹 간접이동 방법
예제
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class IMovementControlObj : MonoBehaviour
{
public float _speed = 5;
Camera _mainCam;
Vector3 _goalPosition;
bool _isMoving = false;
void Start()
{
//GameObject go = GameObject.Find("Main Camera");
//_mainCam = go.GetComponent<Camera>();
_mainCam = Camera.main; // 메인 카메라에 한해서만 사용 가능
_goalPosition = transform.position;
}
void Update()
{
//if (Input.GetMouseButtonDown(0)) // 0: left, 1:right, 2:center
if (Input.GetButtonDown("Fire1")) // Fire1:left, Fire2: right, Fire3: center
{
RaycastHit rHit; // 레이와 부딫힌 오브젝트의 정보
//Ray ray = new Ray(); 일반적인 레이 생성
Ray ray = _mainCam.ScreenPointToRay(Input.mousePosition); // 카메라를 통한 레이 생성
if(Physics.Raycast(ray, out rHit)) // false인 경우 빈 공간 터치한 것
{
//Debug.Log(rHit.transform.name);
//transform.position = rHit.point;
_goalPosition = rHit.point;
_isMoving = true;
}
}
if(_isMoving)
{
//Vector3 dist = _goalPosition - transform.position;
//if (dist.magnitude > 0.25)
//{
// dist.Normalize();
// transform.position += dist * _speed * Time.deltaTime;
//}
//else
//{
// _isMoving = false;
// transform.position = _goalPosition;
//}
transform.position = Vector3.MoveTowards(transform.position, _goalPosition, _speed * Time.deltaTime);
}
//Vector3 dir = (_goalPosition - transform.position).normalized;
//transform.rotation = Quaternion.LookRotation(dir); //or
//transform.LookAt(_goalPosition);
if (_isRotating)
{
Vector3 goalDir = _goalPosition - transform.position;
if (goalDir != Vector3.zero)
{
transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(goalDir), Time.deltaTime * _rotAngle);
}
//if ()
//{
// _isRotating = false;
//}
}
}
}
과제 : 직진만 하되, 회전하면서 직진하도록 만들기(자동차 돌 듯이)