对于3D第一人称控制器游戏,我将转换屏幕上的方向矢量。 一个对象被射向这个方向。 我的相机可以根据虚拟游戏杆的输入进行旋转。 当我不旋转并使用轻扫拍摄物体时,它会朝正确的方向行进。 但是,当我旋转照相机时,它不会朝着预期的方向行进。 方向应适应相机的旋转。我该如何纠正我的矢量方向到相机的旋转?
如何纠正我的向量到相机的旋转方向?
PS:消息我进一步澄清
//Converting swipe direction to 3D direction
public class TouchPair
{
public Vector2 startPos;
public int fingerId;
}
private TouchPair touchPair;
void Update()
{
foreach (Touch touch in Input.touches)
{
Vector2 touchPos = touch.position;
if (touch.phase == TouchPhase.Began)
{
Ray ray = cam.ScreenPointToRay(touchPos);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
//The player is wielding a bomb that is visible on the screen.
//only swipes that start from this object should count
if (hit.transform.tag == "Bomb")
{
touchPair = new TouchPair();
touchPair.startPos = touchPos;
touchPair.fingerId = touch.fingerId;
}
}
}
else if (touch.phase == TouchPhase.Ended)
{
if (touchPair.fingerId == touch.fingerId)
{
Vector2 endPos = touchPos;
Vector2 swipeDirectionRaw = endPos - touchPair.startPos;
float magnitude = swipeDirectionRaw.magnitude;
if (magnitude >= minSwipeLength)
{
BombController BombController = GameObject.FindWithTag("Bomb").GetComponent<BombController>();
BombController.Throw(swipeDirectionRaw.normalized, magnitude);
}
}
}
}
}
public void Throw(Vector2 direction, float magnitude)
{
//Setup variables for throw
throwDirection = new Vector3(direction.x, 0.0f, direction.y);
throwSpeed = magnitude * throwForce;
}
“我正在转换方向矢量在屏幕上的滑动。” - 你能告诉我们这样做的代码吗?我假设如果你使用['Camera.ScreenToWorldPoint'](https://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html),那么你就不会有问题了。 – Quantic