Garzec Geschrieben 19. April 2018 Melden Share Geschrieben 19. April 2018 Hallo, schieße ich mit einem Projektil auf einen Würfel, so sollen über die getroffene Seite folgende Mittelpunkte berechnet werden: gegenüberliegenden Seite linke Seite rechte Seite Die drei neuen Punkte dienen mir danach als Grundlage für Spawnpositionen. Ich habe schon einmal den Code vorbereitet: public class InterferometricCube : MonoBehaviour { [SerializeField] private Vector3 cubeScale; // Größe des Würfels private const float SPAWN_SPACING = 1.5f; // Abstand zum Würfel private void OnCollisionEnter(Collision collision) { CheckProjectile(collision); } private void CheckProjectile(Collision collision) { Projectile projectileComponent = collision.gameObject.GetComponent<Projectile>(); if (projectileComponent) // ist das kollidierende Objekt ein Projektil? { Vector3 contactPoint = collision.contacts.First().normal - transform.up; Vector3[] directions = null; if (contactPoint == new Vector3(0, -1, -1)) // transform.forward // vorne { Vector3[] targetDirections = { -transform.forward, transform.right, -transform.right }; directions = targetDirections; } else if (contactPoint == new Vector3(0, -1, 1)) // -transform.forward // hinten { Vector3[] targetDirections = { transform.forward, transform.right, -transform.right }; directions = targetDirections; } else if (contactPoint == new Vector3(1, -1, 0)) // -transform.right // links { Vector3[] targetDirections = { -transform.forward, transform.right, transform.forward }; directions = targetDirections; } else if (contactPoint == new Vector3(-1, -1, 0)) // transform.right // rechts { Vector3[] targetDirections = { -transform.forward, transform.forward, -transform.right }; directions = targetDirections; } if (directions != null) { SpawnProjectiles(directions); } } } private void SpawnProjectiles(Vector3[] directions) { for (int i = 0; i < directions.Length; i++) { Vector3 currentDirection = directions[i]; // Spawnrotation Vector3 spawnPoint = currentDirection + transform.position + cubeScale; // Spawnposition spawnPoint *= SPAWN_SPACING; // Abstand setzen Debug.Log(spawnPoint); } } } Innerhalb von CheckProjectile berechne ich anhand der Normalen die anderen drei Seiten. Leider springt mir dort der doppelte Code ins Gesicht und möglicherweise kann man dies ja viel einfacher berechnen. Wäre super, falls mir jemand ein wenig mit Mathe aushelfen könnte, falls es einfacher gehen sollte Zitieren Link zu diesem Kommentar Auf anderen Seiten teilen More sharing options...
Garzec Geschrieben 20. April 2018 Autor Melden Share Geschrieben 20. April 2018 Habe es gekürzt bekommen, die Rotationen stimmen aber nicht. public class InterferometricCube : MonoBehaviour { [SerializeField] private Projectile projectileLeft; [SerializeField] private Projectile projectileRight; private void OnCollisionEnter(Collision collision) { HandleProjectile(collision); } private void HandleProjectile(Collision collision) { GameObject collidingObject = collision.gameObject; Projectile projectileComponent = collidingObject.GetComponent<Projectile>(); if (projectileComponent) { Vector3 hitNormal = collision.contacts.First().normal; Vector3 other = Vector3.Cross(hitNormal, collidingObject.transform.up); SpawnProjectile(projectileRight, other); SpawnProjectile(projectileLeft, -other); SpawnProjectile(projectileComponent, -hitNormal); } } private void SpawnProjectile(Projectile projectile, Vector3 direction) { Vector3 spawnPoint = direction + transform.position; projectile.InitProjectile(spawnPoint, Quaternion.Euler(direction)); /* -- InitProjectile -- transform.position = startPosition; transform.rotation = startRotation; currentMovementDirection = transform.forward; */ } } Ich verwende eine TopDown Sicht Egal, von welcher Seite ich auf den Würfel schieße, ein Projektil fliegt immer "nach oben". Das Projektil "nach unten" bleibt im Würfel stecken, da muss ich wohl für die Projektile noch einen Abstand für ihre Achse einberechnen. Das Projektil, mit dem ich auf den Würfel schieße, fliegt idR ebenfalls "nach oben". Hat jemand eine Idee? Zitieren Link zu diesem Kommentar Auf anderen Seiten teilen More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.