我有将对象移动到鼠标X的代码,但是此代码不计算距离(对象在鼠标中直线移动)如何获得它们的控件?玩家将位置移动到偏移量之间而不是紧贴手指位置吗?你能帮我吗?
private void HorizontalMovement()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
{
transform.position = Vector3.Lerp(transform.position, new Vector3(hit.point.x, transform.position.y, transform.position.z), speed * Time.deltaTime);
}
}
答案 0 :(得分:0)
如果我对您的理解是正确的,那么您希望在到达正确的手指位置之前将运动停止一定距离。
// adjust that in the inspector
public float distanceOffset = 0.1f;
private void HorizontalMovement()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
{
// Get the direction from hit object to player
var direction = (new Vector3(transform.position.x - hit.point.x, 0, 0)).normalized;
// Add the offset in the direction to the hit position
var targetPosition = new Vector3(hit.point.x, transform.position.y, transform.position.z) + direction * distanceOffset;
transform.position = Vector3.Lerp(transform.position, targetPosition, speed * Time.deltaTime);
}
}