grounded play no longer speeds up downwards

master
Jordan Orelli 5 years ago
parent 32477f6213
commit 97b713b9fa

@ -59,6 +59,9 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 721dfc7e43d81b644a34024174f78c4e, type: 3} m_Script: {fileID: 11500000, guid: 721dfc7e43d81b644a34024174f78c4e, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
collisionMask:
serializedVersion: 2
m_Bits: 2048
horizontalRayCount: 4 horizontalRayCount: 4
verticalRayCount: 4 verticalRayCount: 4
--- !u!114 &5588890751565926871 --- !u!114 &5588890751565926871
@ -73,7 +76,9 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 7e392b35ca7959444b61a3e29a242f2b, type: 3} m_Script: {fileID: 11500000, guid: 7e392b35ca7959444b61a3e29a242f2b, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
moveSpeed: 6
moveController: {fileID: 0} moveController: {fileID: 0}
velocity: {x: 0, y: 0, z: 0}
--- !u!1 &6383496741659177281 --- !u!1 &6383496741659177281
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

@ -272,12 +272,103 @@ Transform:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1166482433} m_GameObject: {fileID: 1166482433}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: -0.5, z: 0} m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 5, y: 1, z: 1} m_LocalScale: {x: 5, y: 1, z: 1}
m_Children: [] m_Children: []
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_RootOrder: 3 m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1906867256
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1906867260}
- component: {fileID: 1906867259}
- component: {fileID: 1906867258}
- component: {fileID: 1906867257}
m_Layer: 11
m_Name: Cube (1)
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!65 &1906867257
BoxCollider:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1906867256}
m_Material: {fileID: 0}
m_IsTrigger: 0
m_Enabled: 1
serializedVersion: 2
m_Size: {x: 1, y: 1, z: 1}
m_Center: {x: 0, y: 0, z: 0}
--- !u!23 &1906867258
MeshRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1906867256}
m_Enabled: 1
m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:
- {fileID: 2100000, guid: 25b52a028a50a694e898d82345ba62dc, type: 2}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 0
m_SelectedEditorRenderState: 3
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
--- !u!33 &1906867259
MeshFilter:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1906867256}
m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0}
--- !u!4 &1906867260
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1906867256}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 1.12, y: 0.625, z: 0}
m_LocalScale: {x: 0.25, y: 0.25, z: 0.25}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1001 &6383496741781373543 --- !u!1001 &6383496741781373543
PrefabInstance: PrefabInstance:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -298,7 +389,7 @@ PrefabInstance:
- target: {fileID: 6383496741192187647, guid: cd445451907d7274daea79c67e13a202, - target: {fileID: 6383496741192187647, guid: cd445451907d7274daea79c67e13a202,
type: 3} type: 3}
propertyPath: m_LocalPosition.y propertyPath: m_LocalPosition.y
value: 1.11 value: 10
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 6383496741192187647, guid: cd445451907d7274daea79c67e13a202, - target: {fileID: 6383496741192187647, guid: cd445451907d7274daea79c67e13a202,
type: 3} type: 3}
@ -345,32 +436,18 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z propertyPath: m_LocalEulerAnglesHint.z
value: 0 value: 0
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 7623484707498698197, guid: cd445451907d7274daea79c67e13a202,
type: 3}
propertyPath: moveSpeed
value: 10
objectReference: {fileID: 0}
- target: {fileID: 7623484707498698197, guid: cd445451907d7274daea79c67e13a202,
type: 3}
propertyPath: gravityScale
value: 2
objectReference: {fileID: 0}
- target: {fileID: 7623484707498698197, guid: cd445451907d7274daea79c67e13a202,
type: 3}
propertyPath: jumpForce
value: 10
objectReference: {fileID: 0}
- target: {fileID: 7050504111816705868, guid: cd445451907d7274daea79c67e13a202,
type: 3}
propertyPath: m_SkinWidth
value: 0.0125
objectReference: {fileID: 0}
- target: {fileID: 6891912346901077427, guid: cd445451907d7274daea79c67e13a202, - target: {fileID: 6891912346901077427, guid: cd445451907d7274daea79c67e13a202,
type: 3} type: 3}
propertyPath: collisionMask.m_Bits propertyPath: collisionMask.m_Bits
value: 2048 value: 2048
objectReference: {fileID: 0} objectReference: {fileID: 0}
m_RemovedComponents: [] - target: {fileID: 6383496741659177281, guid: cd445451907d7274daea79c67e13a202,
type: 3}
propertyPath: m_IsActive
value: 1
objectReference: {fileID: 0}
m_RemovedComponents:
- {fileID: 528243339991412957, guid: cd445451907d7274daea79c67e13a202, type: 3}
m_SourcePrefab: {fileID: 100100000, guid: cd445451907d7274daea79c67e13a202, type: 3} m_SourcePrefab: {fileID: 100100000, guid: cd445451907d7274daea79c67e13a202, type: 3}
--- !u!1001 &8206000050524585146 --- !u!1001 &8206000050524585146
PrefabInstance: PrefabInstance:

@ -9,6 +9,7 @@ public class MoveController : MonoBehaviour
public const float skinWidth = 0.015f; public const float skinWidth = 0.015f;
public int horizontalRayCount = 4; public int horizontalRayCount = 4;
public int verticalRayCount = 4; public int verticalRayCount = 4;
public CollisionInfo collisions;
new private BoxCollider collider; new private BoxCollider collider;
private RaycastOrigins raycastOrigins; private RaycastOrigins raycastOrigins;
@ -22,33 +23,82 @@ public class MoveController : MonoBehaviour
public void Move(Vector3 velocity) { public void Move(Vector3 velocity) {
UpdateRaycastOrigins(); UpdateRaycastOrigins();
collisions.Reset();
HorizontalCollisions(ref velocity);
VerticalCollisions(ref velocity); VerticalCollisions(ref velocity);
// if (velocity.x != 0) {
// }
// if (velocity.y != 0) {
// }
transform.Translate(velocity); transform.Translate(velocity);
} }
private void HorizontalCollisions(ref Vector3 velocity) {
float directionX = Mathf.Sign(velocity.x); // -1 for left, 1 for right
float rayLength = Mathf.Abs(velocity.x) + skinWidth;
for (int i = 0; i < horizontalRayCount; i++) {
for (int j = 0; j < horizontalRayCount; j++) {
Vector3 rayOrigin = (directionX == -1) ? raycastOrigins.bottomFrontLeft : raycastOrigins.bottomFrontRight;
rayOrigin += Vector3.up * (horizontalRaySpacing * i);
rayOrigin += Vector3.back * (horizontalRaySpacing * j);
RaycastHit[] hits = Physics.RaycastAll(rayOrigin, Vector3.right * directionX, rayLength, collisionMask);
if (hits.Length > 0) {
float minHitDist = hits[0].distance;
foreach(RaycastHit hit in hits) {
if (hit.distance < minHitDist) {
minHitDist = hit.distance;
}
}
velocity.x = (minHitDist - skinWidth) * directionX;
Debug.LogFormat("with RayLength {0} MinHitDist {1} setting velocity.y to {2}", rayLength, minHitDist, velocity.y);
rayLength = minHitDist;
Debug.DrawRay(rayOrigin, Vector3.right * directionX * rayLength, Color.red);
if (directionX == -1) {
collisions.left = true;
} else {
collisions.right = true;
}
} else {
Debug.DrawRay(rayOrigin, Vector3.right * directionX * rayLength, Color.green);
}
}
}
}
private void VerticalCollisions(ref Vector3 velocity) { private void VerticalCollisions(ref Vector3 velocity) {
float directionY = Mathf.Sign(velocity.y); float directionY = Mathf.Sign(velocity.y); // -1 for down, 1 for up
float rayLength = Mathf.Abs(velocity.y) + skinWidth; float rayLength = Mathf.Abs(velocity.y) + skinWidth;
// bottom face
for (int i = 0; i < verticalRayCount; i++) { for (int i = 0; i < verticalRayCount; i++) {
for (int j = 0; j < verticalRayCount; j++) { for (int j = 0; j < verticalRayCount; j++) {
Vector3 rayOrigin = (directionY == -1) ? raycastOrigins.bottomFrontLeft : raycastOrigins.topFrontLeft; Vector3 rayOrigin = (directionY == -1) ? raycastOrigins.bottomFrontLeft : raycastOrigins.topFrontLeft;
rayOrigin += Vector3.right * (verticalRaySpacing * i + velocity.x); rayOrigin += Vector3.right * (verticalRaySpacing * i + velocity.x);
rayOrigin += Vector3.forward * (verticalRaySpacing * j + velocity.x);
RaycastHit[] hits = Physics.RaycastAll(rayOrigin, Vector3.up * directionY, rayLength, collisionMask); RaycastHit[] hits = Physics.RaycastAll(rayOrigin, Vector3.up * directionY, rayLength, collisionMask);
if (hits.Length > 0) { if (hits.Length > 0) {
float minHitDist = 0f; float minHitDist = hits[0].distance;
foreach(RaycastHit hit in hits) { foreach(RaycastHit hit in hits) {
if (hit.distance > minHitDist) { if (hit.distance < minHitDist) {
minHitDist = hit.distance; minHitDist = hit.distance;
} }
} }
velocity.y = (minHitDist - skinWidth) * directionY; velocity.y = (minHitDist - skinWidth) * directionY;
Debug.LogFormat("with RayLength {0} MinHitDist {1} setting velocity.y to {2}", rayLength, minHitDist, velocity.y);
rayLength = minHitDist; rayLength = minHitDist;
Debug.DrawRay(raycastOrigins.bottomFrontLeft + Vector3.right * verticalRaySpacing * i - Vector3.back * verticalRaySpacing * j, Vector3.down * 0.25f, Color.red); Debug.DrawRay(rayOrigin, Vector3.up * directionY * rayLength, Color.red);
if (directionY == -1) {
collisions.below = true;
} else {
collisions.above = true;
}
} else { } else {
Debug.DrawRay(raycastOrigins.bottomFrontLeft + Vector3.right * verticalRaySpacing * i - Vector3.back * verticalRaySpacing * j, Vector3.down * 0.25f, Color.green); Debug.DrawRay(rayOrigin, Vector3.up * directionY * rayLength, Color.green);
} }
} }
} }
@ -90,4 +140,13 @@ public class MoveController : MonoBehaviour
public Vector3 bottomFrontLeft; public Vector3 bottomFrontLeft;
public Vector3 bottomFrontRight; public Vector3 bottomFrontRight;
} }
public struct CollisionInfo {
public bool above;
public bool below;
public bool left;
public bool right;
public void Reset() { above = below = left = right = false; }
}
} }

@ -4,16 +4,27 @@ using UnityEngine;
[RequireComponent(typeof(MoveController))] [RequireComponent(typeof(MoveController))]
public class PlayerController : MonoBehaviour { public class PlayerController : MonoBehaviour {
public float moveSpeed = 6f;
public MoveController moveController; public MoveController moveController;
private float gravity = -20; private float gravity = -20f;
private Vector3 velocity; public Vector3 velocity;
void Start() { void Start() {
moveController = GetComponent<MoveController>(); moveController = GetComponent<MoveController>();
} }
void Update() { void Update() {
if (moveController.collisions.above || moveController.collisions.below) {
velocity.y = 0;
}
if (moveController.collisions.left || moveController.collisions.right) {
velocity.x = 0;
}
Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
velocity.x = input.x * moveSpeed;
velocity.y += gravity * Time.deltaTime; velocity.y += gravity * Time.deltaTime;
moveController.Move(velocity * Time.deltaTime); moveController.Move(velocity * Time.deltaTime);
} }

@ -3,7 +3,7 @@
--- !u!55 &1 --- !u!55 &1
PhysicsManager: PhysicsManager:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
serializedVersion: 8 serializedVersion: 10
m_Gravity: {x: 0, y: -9.81, z: 0} m_Gravity: {x: 0, y: -9.81, z: 0}
m_DefaultMaterial: {fileID: 0} m_DefaultMaterial: {fileID: 0}
m_BounceThreshold: 2 m_BounceThreshold: 2
@ -19,7 +19,7 @@ PhysicsManager:
m_ContactsGeneration: 1 m_ContactsGeneration: 1
m_LayerCollisionMatrix: ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff m_LayerCollisionMatrix: ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
m_AutoSimulation: 1 m_AutoSimulation: 1
m_AutoSyncTransforms: 0 m_AutoSyncTransforms: 1
m_ReuseCollisionCallbacks: 1 m_ReuseCollisionCallbacks: 1
m_ClothInterCollisionSettingsToggle: 0 m_ClothInterCollisionSettingsToggle: 0
m_ContactPairsMode: 0 m_ContactPairsMode: 0
@ -28,3 +28,6 @@ PhysicsManager:
m_Center: {x: 0, y: 0, z: 0} m_Center: {x: 0, y: 0, z: 0}
m_Extent: {x: 250, y: 250, z: 250} m_Extent: {x: 250, y: 250, z: 250}
m_WorldSubdivisions: 8 m_WorldSubdivisions: 8
m_FrictionType: 0
m_EnableEnhancedDeterminism: 0
m_EnableUnifiedHeightmaps: 1

Loading…
Cancel
Save