Du bist nicht angemeldet.

Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!

Werbeanzeige

1

07.06.2016, 08:58

[Unity 5] Rotations und Wheelcolider Problem

Hallo Leute

ich versuche mich etwas in Unity und C Sharp einzuarbeiten und habe dabei auch Hilfe allerdings stehen wir jetzt vor einem Problem.

Wir haben einen Radlader der ja logischerweise eine ARTICULATED Steering hat. Nun haben wir zwar die Knicklenkung hinbekommen aber die Wheelcolider drehen sich einfach weg. sie sollen aber unbeweglich sein auf der Y Achse da die Lenkung ja vom knick kommt. Irgendwo muss doch ein Denkfehler sein.

Ich denke das Bild erklärt das Problem.


(Link)


C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[System.Serializable]
public class AxleInfo
{
    public WheelCollider leftWheel;
    public WheelCollider rightWheel;
    public bool motor;
    public bool steering;
}

public class WheelLoader_Basic : MonoBehaviour
{
    public List<AxleInfo> axleInfos;
    public float maxMotorTorque;
    public float maxSteeringVelocity;
    public Transform frontPivot;
    float lastSteeringValue;
    public float maxSteeringAngle;
    float currentSteeringVelocity = 0f;

    Vector3 currentPivotRotation = Vector3.zero;

    public Transform frontWheels3DLeft;
    public Transform frontWheels3DRight;
    public Transform rearWheels3DLeft;
    public Transform rearWheels3DRight;

    void Awake()
    {
    }

    // finds the corresponding visual wheel
    // correctly applies the transform
    public void ApplyLocalPositionToVisuals(WheelCollider collider, Transform visualWheel)
    {
        Vector3 position;
        Quaternion rotation;
        collider.GetWorldPose(out position, out rotation);

        visualWheel.transform.position = position;
        visualWheel.transform.rotation = rotation;
    }

    public void FixedUpdate()
    {
        float motor = maxMotorTorque * Input.GetAxis("Vertical");
        float steering = maxSteeringVelocity * Input.GetAxis("Horizontal");
        
        currentPivotRotation.y = currentPivotRotation.y + steering * maxSteeringVelocity * Time.fixedDeltaTime;
        if (currentPivotRotation.y <  -maxSteeringAngle)
        {
            currentPivotRotation.y = -maxSteeringAngle;
        }
        if(currentPivotRotation.y > maxSteeringAngle)
        {
            currentPivotRotation.y = maxSteeringAngle;
        }
        frontPivot.localRotation = Quaternion.Euler(currentPivotRotation);

        foreach (AxleInfo axleInfo in axleInfos)
        {
            if(axleInfo.steering)
            {
                if(frontPivot.rotation.eulerAngles.y <= 180f)
                {
                    axleInfo.leftWheel.steerAngle = frontPivot.rotation.eulerAngles.y;
                    axleInfo.rightWheel.steerAngle = frontPivot.rotation.eulerAngles.y;
                }
                else
                {
                    axleInfo.leftWheel.steerAngle = frontPivot.rotation.eulerAngles.y - 360f;
                    axleInfo.rightWheel.steerAngle = frontPivot.rotation.eulerAngles.y - 360f;
                }
            }
            if (axleInfo.motor)
            {
                axleInfo.leftWheel.motorTorque = motor;
                axleInfo.rightWheel.motorTorque = motor;
            }
        }

        ApplyLocalPositionToVisuals(axleInfos[0].leftWheel, frontWheels3DLeft);
        ApplyLocalPositionToVisuals(axleInfos[0].rightWheel, frontWheels3DRight);
        ApplyLocalPositionToVisuals(axleInfos[1].leftWheel, rearWheels3DLeft);
        ApplyLocalPositionToVisuals(axleInfos[1].rightWheel, rearWheels3DRight);
    }
}

Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »Werkstattleiter« (07.06.2016, 09:06)


NachoMan

Community-Fossil

Beiträge: 3 885

Wohnort: Berlin

Beruf: (Nachhilfe)Lehrer (Mathematik, C++, Java, C#)

  • Private Nachricht senden

2

07.06.2016, 15:41

Das Problem ist gelöst.

Ab Zeile 65:

C#-Quelltext

1
2
3
4
5
6
7
8
9
10
if(axleInfo.steering)
{
    axleInfo.leftWheel.steerAngle = currentPivotRotation.y;
    axleInfo.rightWheel.steerAngle = currentPivotRotation.y;
}
if (axleInfo.motor)
{
    axleInfo.leftWheel.motorTorque = motor;
    axleInfo.rightWheel.motorTorque = motor;
}
"Der erste Trunk aus dem Becher der Erkenntnis macht einem zum Atheist, doch auf dem Grund des Bechers wartet Gott." - Werner Heisenberg
Biete Privatunterricht in Berlin und Online.
Kommt jemand mit Nach oMan?

KeksX

Community-Fossil

Beiträge: 2 107

Beruf: Game Designer

  • Private Nachricht senden

3

08.06.2016, 11:18

+1 für das Posten der Lösung. You da real MVP.

Kurzer Tipp vielleicht für den OP/alle anderen die gerne mit so etwas arbeiten möchten:
Im Unity5 Example Project gibt es funktionsfähige Fahrzeuge mit entsprechenden Scripts, die man ganz gut als Grundlage benutzen kann um die Logik dahinter zu verstehen und nachzubasteln.
WIP Website: kevinheese.de

NachoMan

Community-Fossil

Beiträge: 3 885

Wohnort: Berlin

Beruf: (Nachhilfe)Lehrer (Mathematik, C++, Java, C#)

  • Private Nachricht senden

4

08.06.2016, 19:23

Gibt es auch ein Beispiel mit Knicklenkung?

Ich arbeite übrigens mit ihm zusammen und es war mein Fehler. xD
"Der erste Trunk aus dem Becher der Erkenntnis macht einem zum Atheist, doch auf dem Grund des Bechers wartet Gott." - Werner Heisenberg
Biete Privatunterricht in Berlin und Online.
Kommt jemand mit Nach oMan?

Werbeanzeige