Fix slope and character rotation on slopes

This commit is contained in:
2022-08-28 20:55:07 -07:00
parent 41530d5ba4
commit e59eb73745
5 changed files with 196 additions and 29 deletions

View File

@@ -1038,9 +1038,9 @@ CharacterController:
serializedVersion: 2 serializedVersion: 2
m_Height: 3 m_Height: 3
m_Radius: 1 m_Radius: 1
m_SlopeLimit: 75.01 m_SlopeLimit: 75
m_StepOffset: 1.5 m_StepOffset: 1
m_SkinWidth: 0.08 m_SkinWidth: 0.1
m_MinMoveDistance: 0 m_MinMoveDistance: 0
m_Center: {x: 0, y: 0, z: 0} m_Center: {x: 0, y: 0, z: 0}
--- !u!95 &8519787470769432157 --- !u!95 &8519787470769432157

View File

@@ -3322,7 +3322,7 @@ MeshRenderer:
m_PrefabInstance: {fileID: 0} m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1620235925} m_GameObject: {fileID: 1620235925}
m_Enabled: 1 m_Enabled: 0
m_CastShadows: 1 m_CastShadows: 1
m_ReceiveShadows: 1 m_ReceiveShadows: 1
m_DynamicOccludee: 1 m_DynamicOccludee: 1

View File

@@ -579,9 +579,9 @@ Transform:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 211154251} m_GameObject: {fileID: 211154251}
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, z: 0} m_LocalPosition: {x: 95.537, y: 6.483, z: 11.919}
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 0.4818, y: 0.4818, z: 0.4818}
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 1
m_Children: m_Children:
- {fileID: 347813199} - {fileID: 347813199}
- {fileID: 1529204643} - {fileID: 1529204643}
@@ -1884,9 +1884,9 @@ Transform:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 547273015} m_GameObject: {fileID: 547273015}
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, z: 0} m_LocalPosition: {x: 95.537, y: 6.483, z: 11.919}
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 0.4818, y: 0.4818, z: 0.4818}
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 1
m_Children: m_Children:
- {fileID: 545401561} - {fileID: 545401561}
- {fileID: 555652473} - {fileID: 555652473}
@@ -3299,8 +3299,8 @@ Transform:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 887465837} m_GameObject: {fileID: 887465837}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 198, y: 12.510616, z: 23} m_LocalPosition: {x: 190.93, y: 12.511, z: 23}
m_LocalScale: {x: 347.2, y: 347.2, z: 347.2} m_LocalScale: {x: 167.28096, y: 167.28096, z: 167.28096}
m_ConstrainProportionsScale: 1 m_ConstrainProportionsScale: 1
m_Children: [] m_Children: []
m_Father: {fileID: 2100809275} m_Father: {fileID: 2100809275}
@@ -3979,17 +3979,29 @@ PrefabInstance:
propertyPath: m_RootOrder propertyPath: m_RootOrder
value: 0 value: 0
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 0e34d8d9e7b9fd646b2bfb477f56d3af, type: 3}
propertyPath: m_LocalScale.x
value: 0.4818
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 0e34d8d9e7b9fd646b2bfb477f56d3af, type: 3}
propertyPath: m_LocalScale.y
value: 0.4818
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 0e34d8d9e7b9fd646b2bfb477f56d3af, type: 3}
propertyPath: m_LocalScale.z
value: 0.4818
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 0e34d8d9e7b9fd646b2bfb477f56d3af, type: 3} - target: {fileID: -8679921383154817045, guid: 0e34d8d9e7b9fd646b2bfb477f56d3af, type: 3}
propertyPath: m_LocalPosition.x propertyPath: m_LocalPosition.x
value: 0 value: 95.537
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 0e34d8d9e7b9fd646b2bfb477f56d3af, type: 3} - target: {fileID: -8679921383154817045, guid: 0e34d8d9e7b9fd646b2bfb477f56d3af, type: 3}
propertyPath: m_LocalPosition.y propertyPath: m_LocalPosition.y
value: 0 value: 6.483
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 0e34d8d9e7b9fd646b2bfb477f56d3af, type: 3} - target: {fileID: -8679921383154817045, guid: 0e34d8d9e7b9fd646b2bfb477f56d3af, type: 3}
propertyPath: m_LocalPosition.z propertyPath: m_LocalPosition.z
value: 0 value: 11.919
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 0e34d8d9e7b9fd646b2bfb477f56d3af, type: 3} - target: {fileID: -8679921383154817045, guid: 0e34d8d9e7b9fd646b2bfb477f56d3af, type: 3}
propertyPath: m_LocalRotation.w propertyPath: m_LocalRotation.w
@@ -4019,6 +4031,10 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z propertyPath: m_LocalEulerAnglesHint.z
value: 0 value: 0
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 0e34d8d9e7b9fd646b2bfb477f56d3af, type: 3}
propertyPath: m_ConstrainProportionsScale
value: 1
objectReference: {fileID: 0}
- target: {fileID: -1853094968961178454, guid: 0e34d8d9e7b9fd646b2bfb477f56d3af, type: 3} - target: {fileID: -1853094968961178454, guid: 0e34d8d9e7b9fd646b2bfb477f56d3af, type: 3}
propertyPath: m_ClearFlags propertyPath: m_ClearFlags
value: 3 value: 3
@@ -5310,6 +5326,18 @@ PrefabInstance:
propertyPath: m_Enabled propertyPath: m_Enabled
value: 1 value: 1
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: -7493596028755563170, guid: 325339469f6b77d4a8f54684fbc34abc, type: 3}
propertyPath: m_LocalPosition.x
value: -80.4
objectReference: {fileID: 0}
- target: {fileID: -7493596028755563170, guid: 325339469f6b77d4a8f54684fbc34abc, type: 3}
propertyPath: m_LocalPosition.y
value: 14.4
objectReference: {fileID: 0}
- target: {fileID: -7493596028755563170, guid: 325339469f6b77d4a8f54684fbc34abc, type: 3}
propertyPath: m_LocalPosition.z
value: 38.6
objectReference: {fileID: 0}
- target: {fileID: -1853094968961178454, guid: 325339469f6b77d4a8f54684fbc34abc, type: 3} - target: {fileID: -1853094968961178454, guid: 325339469f6b77d4a8f54684fbc34abc, type: 3}
propertyPath: m_Enabled propertyPath: m_Enabled
value: 1 value: 1
@@ -5326,6 +5354,18 @@ PrefabInstance:
propertyPath: m_Enabled propertyPath: m_Enabled
value: 1 value: 1
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 53111293769073355, guid: 325339469f6b77d4a8f54684fbc34abc, type: 3}
propertyPath: m_LocalPosition.x
value: 30.31
objectReference: {fileID: 0}
- target: {fileID: 53111293769073355, guid: 325339469f6b77d4a8f54684fbc34abc, type: 3}
propertyPath: m_LocalPosition.y
value: 14.16
objectReference: {fileID: 0}
- target: {fileID: 53111293769073355, guid: 325339469f6b77d4a8f54684fbc34abc, type: 3}
propertyPath: m_LocalPosition.z
value: -4.27
objectReference: {fileID: 0}
- target: {fileID: 65702495048144492, guid: 325339469f6b77d4a8f54684fbc34abc, type: 3} - target: {fileID: 65702495048144492, guid: 325339469f6b77d4a8f54684fbc34abc, type: 3}
propertyPath: m_IsActive propertyPath: m_IsActive
value: 1 value: 1

View File

@@ -1,5 +1,4 @@
using Scampz.GameJam.Assets.Scripts.Player; using UnityEngine;
using UnityEngine;
namespace Scampz.GameJam.Assets.Scripts namespace Scampz.GameJam.Assets.Scripts
{ {
@@ -8,29 +7,60 @@ namespace Scampz.GameJam.Assets.Scripts
private CharacterController _controller; private CharacterController _controller;
public float Speed = 10f; public float Speed = 10f;
public float RotateSpeed = 720.0f; public float RotateSpeed = 720.0f;
private PlayerState _playerState; private float _ySpeed = 0f;
private void Start() private void Start()
{ {
_controller = GetComponent<CharacterController>(); _controller = GetComponent<CharacterController>();
_playerState = GetComponent<PlayerState>();
} }
void Update() void Update()
{ {
//Physics.Raycast(transform.position, -transform.up, out var hit, 10f); Move();
//var slope = hit.normal.y; }
var direction = new Vector3(Input.GetAxisRaw(InputOptions.Horizontal), 0, Input.GetAxisRaw(InputOptions.Vertical)).normalized;
if (_playerState.CanMove) void Move()
_controller.SimpleMove(-direction * Speed); {
var horizontalMovement = Input.GetAxisRaw(InputOptions.Horizontal);
var verticalMovement = Input.GetAxisRaw(InputOptions.Vertical);
if (direction != Vector3.zero) var movementDirection = new Vector3(-horizontalMovement, 0, -verticalMovement);
var magnitude = Mathf.Clamp01(movementDirection.magnitude) * Speed;
movementDirection.Normalize();
_ySpeed += Physics.gravity.y * Time.deltaTime;
if (_controller.isGrounded)
_ySpeed -= 0.5f;
var velocity = movementDirection * magnitude;
velocity = AdjustVelocityToSlope(velocity);
velocity.y += _ySpeed;
_controller.Move(velocity * Time.deltaTime);
if (movementDirection != Vector3.zero)
{ {
var toRotation = Quaternion.LookRotation(-direction, Vector3.up); var ray = new Ray(transform.position, Vector3.down);
Physics.Raycast(ray, out var hitInfo, 5f);
transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, RotateSpeed); var toRotation = Quaternion.LookRotation(movementDirection, Vector3.up);
toRotation.x = Quaternion.LookRotation(Vector3.Cross(transform.right, hitInfo.normal)).x;
transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, RotateSpeed * Time.deltaTime);
} }
} }
private Vector3 AdjustVelocityToSlope(Vector3 velocity)
{
var ray = new Ray(transform.position, Vector3.down);
if (Physics.Raycast(ray, out var hitInfo, 5f))
{
var slopeRotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
var adjustedVelocity = slopeRotation * velocity;
if (adjustedVelocity.y < 0)
return adjustedVelocity;
}
return velocity;
}
} }
} }

View File

@@ -269,7 +269,104 @@ PlayerSettings:
AndroidValidateAppBundleSize: 1 AndroidValidateAppBundleSize: 1
AndroidAppBundleSizeToValidate: 150 AndroidAppBundleSizeToValidate: 150
m_BuildTargetIcons: [] m_BuildTargetIcons: []
m_BuildTargetPlatformIcons: [] m_BuildTargetPlatformIcons:
- m_BuildTarget: iPhone
m_Icons:
- m_Textures: []
m_Width: 180
m_Height: 180
m_Kind: 0
m_SubKind: iPhone
- m_Textures: []
m_Width: 120
m_Height: 120
m_Kind: 0
m_SubKind: iPhone
- m_Textures: []
m_Width: 167
m_Height: 167
m_Kind: 0
m_SubKind: iPad
- m_Textures: []
m_Width: 152
m_Height: 152
m_Kind: 0
m_SubKind: iPad
- m_Textures: []
m_Width: 76
m_Height: 76
m_Kind: 0
m_SubKind: iPad
- m_Textures: []
m_Width: 120
m_Height: 120
m_Kind: 3
m_SubKind: iPhone
- m_Textures: []
m_Width: 80
m_Height: 80
m_Kind: 3
m_SubKind: iPhone
- m_Textures: []
m_Width: 80
m_Height: 80
m_Kind: 3
m_SubKind: iPad
- m_Textures: []
m_Width: 40
m_Height: 40
m_Kind: 3
m_SubKind: iPad
- m_Textures: []
m_Width: 87
m_Height: 87
m_Kind: 1
m_SubKind: iPhone
- m_Textures: []
m_Width: 58
m_Height: 58
m_Kind: 1
m_SubKind: iPhone
- m_Textures: []
m_Width: 29
m_Height: 29
m_Kind: 1
m_SubKind: iPhone
- m_Textures: []
m_Width: 58
m_Height: 58
m_Kind: 1
m_SubKind: iPad
- m_Textures: []
m_Width: 29
m_Height: 29
m_Kind: 1
m_SubKind: iPad
- m_Textures: []
m_Width: 60
m_Height: 60
m_Kind: 2
m_SubKind: iPhone
- m_Textures: []
m_Width: 40
m_Height: 40
m_Kind: 2
m_SubKind: iPhone
- m_Textures: []
m_Width: 40
m_Height: 40
m_Kind: 2
m_SubKind: iPad
- m_Textures: []
m_Width: 20
m_Height: 20
m_Kind: 2
m_SubKind: iPad
- m_Textures: []
m_Width: 1024
m_Height: 1024
m_Kind: 4
m_SubKind: App Store
m_BuildTargetBatching: m_BuildTargetBatching:
- m_BuildTarget: Standalone - m_BuildTarget: Standalone
m_StaticBatching: 1 m_StaticBatching: 1