Für ein 3D-First-Person-Controller-Spiel, konvertiere ich einen Swipe auf dem Bildschirm in einem Richtungsvektor. Ein Objekt wird in diese Richtung geschossen. Meine Kamera kann basierend auf der Eingabe eines virtuellen Joysticks drehen. Wenn ich nicht rotiere und das Objekt mit dem Swipe schieße, geht es in die richtige Richtung. Allerdings, wenn ich die Kamera rotiere geht es nicht in die beabsichtigte Richtung. Die Richtung sollte an die Drehung der Kamera angepasst werden.Wie korrigiere ich die Richtung meines Vektors zur Drehung der Kamera?
Wie korrigiere ich die Richtung meines Vektors zur Drehung der Kamera?
PS: Mitteilung ich für eine weitere Klarstellung
//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;
}
"Ich konvertiere einen Swipe auf dem Bildschirm in einem Richtungsvektor." - Können Sie uns den Code zeigen, der das macht? Ich nehme an, dass Sie kein Problem hätten, wenn Sie ['Camera.ScreenToWorldPoint'] (https://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html) verwenden würden. – Quantic