Jump to content
Unity Insider Forum

Falsche Position der Maus Position.


Dog

Recommended Posts

Einen schönen guten Abend, dies ist mein erster Beitrag hier im Forum.
Zunächst möchte ich erwähnen das ich in dem Bereich noch ein Anfänger bin!
Diese Information gebe ich damit ihr euch nicht Wundert oder schockiert seit wenn dieser Code evtl. totaler mist ist.

Ich sitze aktuell an ein Building/Placement Script womit man wie in "Prison Achitect" , "Sim Airport" o.ä. Böden,Wände etc. platzieren kann in verschiedenen größen.
Also ich drücke mit der Maustaste und ziehe ein Feld wo dann zum Beispiel ein Boden platziert wird.

Das ganze habe ich wie folgt hinbekommen:

void Startpunkt()
{
    TempPlacement = true;
    Startposition = gridSnap(AktuellePosition());
    //Gridstart = gridSnap(AktuellePosition());
}
void Endpunkt()
{
    TempPlacement = false;
    Endposition = gridSnap(AktuellePosition());   
    //Gridstart = Vector3.zero;
    Platzierung();
} 

void Platzierung()
{
    if (isBuildMode && SelectBoden != null)
    {
        if (Endposition != new Vector3(0.0f, 0.0f, 0.0f) && Startposition != new Vector3(0.0f, 0.0f, 0.0f))
        {
            Vector3 Differenz;
            Debug.Log(Startposition);
            Debug.Log(Endposition);
            Differenz.x = Vector3.Distance(new Vector3(Startposition.x , 0, 0), new Vector3(Endposition.x, 0, 0));
            Differenz.z = Vector3.Distance(new Vector3(0, 0, Startposition.z), new Vector3(0, 0, Endposition.z));    
            Debug.Log("Differenz.x = " + Differenz.x + " | Differenz.z = " + Differenz.z);

            for (int x = -1; x < Differenz.x; x++)
            {
                for (int y = -1; y < Differenz.z; y++)
                {
                    Vector3 TempPosition = new Vector3(Startposition.x + x, Startposition.y, Startposition.z + y);
                    GameObject TempObjekt = Instantiate(SelectBoden, TempPosition, Quaternion.identity);
                }
            }
        }
    }
}

public Vector3 AktuellePosition()
{
  	Vector3 Maus = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z);
  	Ray ray = Camera.main.ScreenPointToRay(Maus); RaycastHit hit;
  	
  	if (Physics.Raycast(ray, out hit)){return hit.point;}
  	return Vector3.zero;
}
public Vector3 gridSnap(Vector3 OriginalPosition)//Tutorial unitylession
{
  	float granularity = 1f;
  	Vector3 snappedPosition = new Vector3(Mathf.Floor(OriginalPosition.x / granularity) * granularity, OriginalPosition.y, Mathf.Floor(OriginalPosition.z / granularity) * granularity);
  	return snappedPosition;
}

Der Startpunkt wird bei GetMouseButtonDown aufgerufen und der Endpunkt bei  GetMouseButtonUp.

Das ganze klappt soweit auch jedoch stimmt die Position nicht mit der Maus Position.
Sobald ich in dem Spiel irgendwo hinklicke wird das Objekt oder die Objekte weiter weg gesetzt.

Unity_2017-10-05_20-09-32.jpg.7b0bc65624ce857927fec13ce51a73a0.jpgUnity_2017-10-05_20-47-54.jpg.266c02257915d5c55c899b0a1017a4ef.jpg

Dort wo die Maus ist klickte ich einmal und der/die grüne(n) Quadrat(e) spawnt/spawnen dann halt ein Feld  oder zwei weiter weg von der Maus.
Ich bedanke mich schon einmal für das Lesen und hoffe ihr könnt mir helfen.

Lg. Dog

Link zu diesem Kommentar
Auf anderen Seiten teilen

Schwer zu sagen, schau mal wo der Ray "einschlägt" und ob diese Position passt:

public Vector3 AktuellePosition()
{
  	Vector3 Maus = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z);
  	Ray ray = Camera.main.ScreenPointToRay(Maus); RaycastHit hit;
  	
  	if (Physics.Raycast(ray, out hit)) {
           Debug.DrawRay(hit.point - ray.direction * 2, ray.direction * 4, Color.yellow, 5f, false);
           return hit.point;
        }
  	return Vector3.zero;
}

 

Link zu diesem Kommentar
Auf anderen Seiten teilen

DrawRay.png.501df5b47e041bbbf3c2c8794cec9817.png

Das passt mit der Maus Position überein.
Musste nur pausieren da man den Ray bei TopDown nicht sieht.

 

 

*Edit:
Also es muss glaube an diesen 2 schleifen liegen

for (int x = -1; x < Differenz.x; x++)
{
    for (int y = -1; y < Differenz.z; y++)
    {
        if(x == -1) { x = 0; }
        if(y == -1) { y = 0; }
        Vector3 TempPosition = new Vector3(Startposition.x + x, Startposition.y, Startposition.z + y);
        GameObject TempObjekt = Instantiate(SelectBoden, TempPosition, Quaternion.identity);
    }
}

Sobald ich die "-1" nicht in 0 umwandel setzt er bei einem einfachen Mausklick dieses Objekt an die exakte Position.
Wenn ich dann aber ein Feld mit gedrückter Maustaste ziehe ist das ganze Feld verschoben.
Wenn ich in den schleifen es auf "0" stelle kommt kein Objekt bei einem einfachen Mausklick.

 

*Edit2:
Ok ich habe es jetzt mit einer anderen Variante hinbekommen.

void Platzierung()
    {
        if (isBuildMode && SelectBoden != null)
        {
            if (Endposition != new Vector3(0.0f, 0.0f, 0.0f) && Startposition != new Vector3(0.0f, 0.0f, 0.0f))
            {
                bool aNegativ = false;
                bool bNegativ = false;
                float tLänge = Startposition.x - Endposition.x;
                float tBreite = Startposition.z - Endposition.z;
                Vector3 tWert;

                //Rundung:
                tLänge = Mathf.Round(tLänge);
                tBreite = Mathf.Round(tBreite);

                Debug.Log("Startposition: " + Startposition);
                Debug.Log("tLänge: " + tLänge + " | tBreite: " + tBreite);

                //Prüfen ob der Wert Negativ ist, wenn ja dann Umwandeln in einen Positiven Wert:
                if (Mathf.Sign(tLänge) == -1)  { aNegativ = true; tLänge = Mathf.Abs(tLänge); }
                if (Mathf.Sign(tBreite) == -1) { bNegativ = true; tBreite = Mathf.Abs(tBreite); }

                Debug.Log("boolA: "+ aNegativ+ " |boolB: " + bNegativ+ " | ABS-tLänge: " + tLänge + " | ABS-tBreite: " + tBreite);

                //Schleifen:
                for (int x = -1; x < tLänge; x++)
                {
                    for (int z = -1; z < tBreite; z++)
                    {
                        if (x == -1) { x = 0; }
                        if (z == -1) { z = 0; }
                        //Wert x,y wird bei Negativ wieder Negativ gesetzt:
                        if (aNegativ) { tWert.x = Startposition.x + x; } else { tWert.x = Startposition.x - x; }
                        if (bNegativ) { tWert.z = Startposition.z + z; } else { tWert.z = Startposition.z - z; }
                        Vector3 TempPosition = new Vector3(tWert.x, Startposition.y, tWert.z);
                        Debug.Log("TempPosition: " + TempPosition);

                        GameObject TempObjekt = Instantiate(SelectBoden, TempPosition, Quaternion.identity);
                    }
                }
            }
        }
    }

DrawRay2.png.d94c8944f916bb4e0a282a9c28eecc0b.png

 

Erstmal auch vielen Dank für die Antwort :)

Link zu diesem Kommentar
Auf anderen Seiten teilen

Archiviert

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

×
×
  • Neu erstellen...