Jump to content
Unity Insider Forum

Bewegung abhängig von Camera


Ich

Recommended Posts

"Vorne" der Kamera kannst du abfragen durch

Camera.main.transform.forward

Da die Kamera ja aber auch etwas nach oben oder unten gucken kann, solltest du den Y-Wert dieses Vektors auf 0 setzen, um einen horizontalen Vektor zu erhalten.

Könnte also insgesamt etwa so aussehen:

public void KickBall()
{
  var direction = Camera.main.transform.forward;
  direction.y = 0;
  ball.AddForce(direction.normalized * power, ForceMode.Impulse);
}

 

Link zu diesem Kommentar
Auf anderen Seiten teilen

Ich verstehe nicht wie ich das benutzen soll 😅

Das ist mein Script

    void Update()
    {
        if (!scr_BasketBall.bl_throw)
        {
            if (Input.GetMouseButtonDown(0))
            {
                press = true;
                startInput = Camera.main.ScreenToViewportPoint(Input.mousePosition);
            }
            if (Input.GetMouseButton(0))
            {
                if (startInput == Vector3.zero)
                {
                    press = true;
                    startInput = Camera.main.ScreenToViewportPoint(Input.mousePosition);
                }
                dirInput = Vector3.Normalize(Camera.main.ScreenToViewportPoint(Input.mousePosition) - startInput);
                dirInput.z = dirInput.y;
                dirInput.y = 0;
            }
            if (Input.GetMouseButtonUp(0))
            {
                press = false;
                startInput = Vector3.zero;
            }
            if (!press)
            {
                Vector3 temp = rb.velocity * 0.95f;
                temp.y = rb.velocity.y;
                rb.velocity = temp;
            }
        }
    }

    private void FixedUpdate()
    {
        if (press)
        {
            v3_mag.x = rb.velocity.x + dirInput.x * speed;
            v3_mag.z = rb.velocity.z + dirInput.z * speed;
            v3_mag.y = 0;

            v3_mag = Vector3.ClampMagnitude(v3_mag, maxSpeed);
            v3_mag.y = rb.velocity.y;
            rb.velocity = v3_mag;

            v3_mag.y = 0;
        }
    }

 

Link zu diesem Kommentar
Auf anderen Seiten teilen

Archiviert

Dieses Thema ist jetzt archiviert und für weitere Antworten gesperrt.

×
×
  • Neu erstellen...