【发布时间】:2019-09-11 14:23:28
【问题描述】:
我一直在尝试让 2D 玩家像子弹一样一直向前移动(在这种情况下,向前是游戏对象的局部 X 轴,因为这就是角色的方式面对)并且仅在您触摸屏幕上的某个点时改变方向,在这种情况下,它应该会平稳地开始转向该点。
我遇到的一个问题是我无法让角色在它之前面对的最后一个方向上以恒定的速度平稳移动,而我发现的另一个问题是角色正在转身错误的轴,而不是基于 Z 轴旋转,而是始终在 Y 轴上旋转,这使得精灵对相机变得不可见。
这是我现在拥有的代码:
Vector3 lastTouchPoint;
private void Start()
{
lastTouchPoint = transform.position;
}
void Update()
{
if (Input.touchCount > 0)
{
// The screen has been touched so store the touch
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved)
{
// If the finger is on the screen, move the object smoothly to the touch position
lastTouchPoint = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10));
}
}
transform.position = Vector3.Lerp(transform.position, lastTouchPoint, Time.deltaTime);
//Rotate towards point
Vector3 targetDir = lastTouchPoint - transform.position;
transform.LookAt(lastTouchPoint);
}
提前致谢!
【问题讨论】:
-
问题是您不希望精灵实际上以 3d 方式移动,这就是您对 lerp 等所做的。您只希望它围绕指向相机的 z 轴旋转。您可能希望它的位置移动,而不是恶意指向的位置。
-
我将如何实现这一目标? @BugFinder