對於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