3D初心者のコントローラのゲームでは、画面上のスワイプを方向ベクトルに変換しています。 オブジェクトがこの方向に射撃されます。 私のカメラは、仮想ジョイスティックの入力に基づいて回転できます。 スワイプを使用してオブジェクトを回転させたり撮影したりしないと、正しい方向に移動します。 しかし、カメラを回転させると、意図した方向には移動しません。 方向はカメラの回転に合わせる必要があります。ベクトルの向きをカメラの回転に合わせるにはどうすればよいですか?
ベクターのカメラの回転方向を修正するにはどうすればよいですか?
PS:touch.phaseは== TouchPhase.Endedその後、raycast.hitためにあなたのキャラクターからの方向を取得するときに、さらに明確化
//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