Skip to content

Commit

Permalink
0.3.0p2
Browse files Browse the repository at this point in the history
Robot doesn't die anymore when it flips over.
  • Loading branch information
griffinteller committed May 9, 2020
1 parent 605a665 commit 87c71f8
Show file tree
Hide file tree
Showing 284 changed files with 8,629 additions and 64,761 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ MonoBehaviour:
- ClickRpc
- DestroyRpc
DisableAutoOpenWizard: 1
ShowSettings: 0
ShowSettings: 1
DevRegionSetOnce: 1
3 changes: 2 additions & 1 deletion Assets/Prefab/Part/Play/BasicTire.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
innerMesh: {fileID: 212465993580246236}
_tireComponent: {fileID: 4734048834668821019}
tireMeshRoot: {fileID: 0}
wheelCollider: {fileID: 0}
--- !u!114 &4734048834668821019
Expand All @@ -250,5 +251,5 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 1416999b371041a993ba1d4cc6fa7c56, type: 3}
m_Name:
m_EditorClassIdentifier:
wheelCollider: {fileID: 0}
bearing: 0
baseSteerAngle: 0
2 changes: 1 addition & 1 deletion Assets/Scenes/MainMenuScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -1338,7 +1338,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 519420028}
m_LocalRotation: {x: 0.11222858, y: -0, z: -0, w: 0.99368244}
m_LocalPosition: {x: -0.13, y: 1.71, z: -3.1}
m_LocalPosition: {x: -0.13, y: 2.35, z: -3.1}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
Expand Down
26 changes: 22 additions & 4 deletions Assets/Scripts/Main/ActionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ private Vector3

private readonly Dictionary<string, TireComponent> _tireComponents = new Dictionary<string, TireComponent>();

private bool _waiting;

// Start is called before the first frame update
private void Start()
{
Expand Down Expand Up @@ -74,6 +76,13 @@ public void Update()

case ResetState.NeedToUndoReset:

if (!_waiting)
{
_waiting = true;
break;
}

_waiting = false;
UndoReset();
_resetState = ResetState.Normal;
break;
Expand Down Expand Up @@ -110,28 +119,37 @@ private void InternalResetRobot()
_rigidbody.angularVelocity = Vector3.zero;
_rigidbody.isKinematic = true;

internalNegation = 1;

foreach (var tire in _tireComponents.Values)
{
tire.WheelCollider.motorTorque = 0;
tire.bearing = 0;
tire.WheelCollider.brakeTorque = 10000;
tire.WheelCollider.brakeTorque = 10000000000000000;
tire.ResetTireSteering();
}
}

public void SetTireTorque(string tireName, float torque)
{

var tireObject = _tireComponents[tireName + "Vis"];
tireObject.WheelCollider.motorTorque = torque * internalNegation; //* userNegation;
tireObject.WheelCollider.motorTorque = torque * internalNegation;

}

public void InvertTires()
public void AdjustTireOrientation()
{
foreach (var tire in _tireComponents.Values)
{
tire.WheelCollider.motorTorque *= -1;
tire.bearing *= -1;

var correction = 0;

if (internalNegation == -1)
correction = 180;

tire.baseSteerAngle = (tire.originalSteerAngle + correction) % 360;
}
}

Expand Down
17 changes: 17 additions & 0 deletions Assets/Scripts/Main/TireComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ public class TireComponent : MonoBehaviour

public float baseSteerAngle;

public float originalSteerAngle;

public void Start()
{

originalSteerAngle = baseSteerAngle;

}

public void ResetTireSteering()
{

baseSteerAngle = originalSteerAngle;
bearing = 0;

}

public void Update()
{

Expand Down
18 changes: 17 additions & 1 deletion Assets/Scripts/Main/TireMeshAnimator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@ public class TireMeshAnimator : MonoBehaviour
private const float LerpTimePerRotation = 1f;

[SerializeField] private Transform innerMesh;
[SerializeField] private TireComponent _tireComponent;

[HideInInspector] public Transform tireMeshRoot;
[HideInInspector] public WheelCollider wheelCollider;

private Vector3 _desiredLocalPosition; // to facilitate lerping
private Quaternion _desiredLocalRotation;
private ActionHandler _actionHandler;

public void Start()
{

_actionHandler = transform.root.GetComponent<ActionHandler>();

RotateInnerTireFromRpm();

Expand Down Expand Up @@ -51,8 +55,20 @@ private void RotateInnerTireFromRpm()

private void SyncDesiredRotationWithSteerAngle()
{

var correction = 0;

if (_actionHandler.internalNegation == -1)
{

correction = 180;

}

_desiredLocalRotation = Quaternion.Euler(0, wheelCollider.steerAngle, 0);
_desiredLocalRotation = Quaternion.Euler(
0,
_tireComponent.originalSteerAngle + _actionHandler.internalNegation * _tireComponent.bearing,
0);

}

Expand Down
31 changes: 16 additions & 15 deletions Assets/Scripts/Main/WheelColliderFlippingBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Main
public class WheelColliderFlippingBehaviour : MonoBehaviour
{
private ActionHandler _actionHandler;
private bool _flipped;
private bool _flippedLastFrame;

private Transform _robotBody;

Expand All @@ -31,25 +31,26 @@ public void Update()
upVectorCorrectionMagnitude, robotBodyRightVector);

var innerRotation = robotBodyTransform.rotation;
var outerRotation = upVectorCorrection * innerRotation;

transform.rotation = upVectorCorrection * innerRotation;
if (Vector3.Angle(robotBodyTransform.forward, outerRotation * Vector3.forward) > 90)
outerRotation = Quaternion.AngleAxis(180, outerRotation * Vector3.up) * outerRotation;

transform.rotation = outerRotation;
robotBodyTransform.rotation = innerRotation; // fixes inner rotation because it gets pulled along with its parent
var flippedCurrently = robotBodyTransform.up.y < 0;

if (robotBodyTransform.up.y < 0)
if (flippedCurrently != _flippedLastFrame)
{
_actionHandler.internalNegation = -1;

if (!_flipped)
{
_actionHandler.InvertTires();
_flipped = true;
}
}
else
{
_actionHandler.internalNegation = 1;
_flipped = false;
if (flippedCurrently)
_actionHandler.internalNegation = -1;
else
_actionHandler.internalNegation = 1;

_actionHandler.AdjustTireOrientation();
_flippedLastFrame = flippedCurrently;
}

}
}
}
2 changes: 1 addition & 1 deletion Assets/Scripts/Utility/TerrainUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static Vector3 GetTerrainNormal(Vector3 worldPosition, float sampleRadius
var zHigh = terrain.SampleHeight(worldPosition + Vector3.forward * sampleRadius);

var xDisplacement = new Vector3(sampleRadius * 2,xHigh - xLow, 0);
var zDisplacement = new Vector3(0,xHigh - xLow, sampleRadius * 2);
var zDisplacement = new Vector3(0,zHigh - zLow, sampleRadius * 2);

return Vector3.Cross(zDisplacement, xDisplacement).normalized;

Expand Down
1 change: 1 addition & 0 deletions ProjectSettings/GraphicsSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ GraphicsSettings:
- {fileID: 16000, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 16001, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 17000, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 16002, guid: 0000000000000000f000000000000000, type: 0}
m_PreloadedShaders: []
m_SpritesDefaultMaterial: {fileID: 10754, guid: 0000000000000000f000000000000000,
type: 0}
Expand Down
2 changes: 1 addition & 1 deletion ProjectSettings/ProjectSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ PlayerSettings:
16:10: 1
16:9: 1
Others: 1
bundleVersion: 0.3.0p1
bundleVersion: 0.3.0p2
preloadedAssets:
- {fileID: 0}
- {fileID: 0}
Expand Down
Loading

0 comments on commit 87c71f8

Please sign in to comment.