마우스 커서를 사용하여 우주 게임 및 우주선 객체 (플레이어)의 움직임을 작성하고 있습니다.Unity : 게임용 특수 동작
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class Move : MonoBehaviour {
public float speed = 1.5f;
public float rotationSpeed = 90f;
public float rotPrecision = 0.1f;
public float movePrecision = 0.1f;
private Vector3 pos;
private Quaternion qTo;
void Start() {
pos = transform.position;
qTo = transform.rotation;
}
void Update() {
if (!EventSystem.current.IsPointerOverGameObject())
{
if (Input.GetMouseButtonDown(0) || Input.GetMouseButton(0))
{
pos = Input.mousePosition;
pos.z = transform.position.z - Camera.main.transform.position.z;
pos = Camera.main.ScreenToWorldPoint(pos);
}
var dir = pos - transform.position;
qTo = Quaternion.LookRotation(Vector3.forward, pos - transform.position);
if (Quaternion.Angle(transform.rotation, qTo) >= rotPrecision) //just set your own precision
transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, Time.deltaTime * rotationSpeed);
if (Vector3.Distance(transform.position, pos) > movePrecision) // 0.1f
transform.Translate(Vector3.up * speed * Time.deltaTime);
}
}
}
을하지만이 난 지점이 플레이어에 너무 가까이있을 때 이동 정밀도와 회전의 문제가 (무한 루프가) :
현재 다음 코드가 있습니다.
다음 이미지와 설명이 운동 시스템의 개념 :
(플레이어의 배우가 녹색, 경로는 회색이며, 대상 지점은 빨간색입니다).
나는 누군가가 그 승/나를 도울 수 있기를 바랍니다.
감사합니다.
정확도가 의미하는 몇 가지 코드 예제 나 설명을 공유해 주시겠습니까? – featureoffuture
현재 사용중인 코드가 이미 정밀도 계산을 시도하고있는 것 같습니다. rotPrecision 및 movePrecision 값을 더 높은 값으로 조정 해보십시오. 코드 내부가 아닌 검사기에서 수행해야합니다. – Doh09
나는 아무것도 시도했지만 ... 아무 것도 제대로 작동하지 않습니다 ... – featureoffuture