Skip to content

Commit 783d225

Browse files
committed
Wall hit
1 parent b74f9f8 commit 783d225

File tree

6 files changed

+90
-12
lines changed

6 files changed

+90
-12
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:567ae3f9be0eca23fae93bea19f178a7fcb8cf06b9a8020c2711d38b6b041254
3+
size 305768

unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Avatar/Animations/Addressables/gltf/Hit_Wall_Walk.glb.meta

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/CharacterControllerV2/Animation/CharacterAnimationController.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class CharacterAnimationController : MonoBehaviour, IAnimator
1818
private static readonly int FALLING = Animator.StringToHash("IsFalling");
1919
private static readonly int LONG_JUMP = Animator.StringToHash("IsLongJump");
2020
private static readonly int JUMP = Animator.StringToHash("Jump");
21+
private static readonly int WALL_HIT = Animator.StringToHash("WallHit");
2122
private static readonly int EMOTE_REFRESH = Animator.StringToHash("EmoteRefresh");
2223
private static readonly int ANGLE = Animator.StringToHash("Angle");
2324
private static readonly int ANGLE_DIR = Animator.StringToHash("AngleDir");
@@ -76,9 +77,12 @@ public void SetupCharacterState(CharacterState characterState)
7677
{
7778
this.characterState = characterState;
7879
characterState.OnJump += OnJump;
80+
characterState.OnWallHit += OnWallHit;
81+
characterState.OnWallHitReset += OnWallHitReset;
7982
}
8083

8184
// todo: move this elsewhere
85+
8286
private void InitializeAvatarAudioAndParticleHandlers(GameObject container)
8387
{
8488
AvatarAnimationEventHandler animationEventHandler = container.GetOrCreateComponent<AvatarAnimationEventHandler>();
@@ -100,6 +104,16 @@ private void OnJump()
100104
animator.SetTrigger(JUMP);
101105
}
102106

107+
private void OnWallHit()
108+
{
109+
animator.SetTrigger(WALL_HIT);
110+
}
111+
112+
private void OnWallHitReset()
113+
{
114+
animator.ResetTrigger(WALL_HIT);
115+
}
116+
103117
private void LateUpdate()
104118
{
105119
if (characterState == null || animator == null) return;

unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/CharacterControllerV2/Animation/CharacterState.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public class CharacterState
1010
public SpeedState SpeedState;
1111
public bool IsGrounded;
1212
public event Action OnJump = () => { };
13+
public event Action OnWallHit = () => { };
14+
public event Action OnWallHitReset = () => { };
1315
public bool IsJumping;
1416
public bool IsLongJump;
1517
public bool IsLongFall;
@@ -24,5 +26,15 @@ public void Jump()
2426
{
2527
OnJump.Invoke();
2628
}
29+
30+
public void WallHit()
31+
{
32+
OnWallHit.Invoke();
33+
}
34+
35+
public void ResetWallHit()
36+
{
37+
OnWallHitReset.Invoke();
38+
}
2739
}
2840
}

unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/CharacterControllerV2/CharacterView.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,20 +171,20 @@ private Vector3 Flat(Vector3 vector3)
171171
return vector3;
172172
}
173173

174-
public (bool isGrounded, Vector3 deltaPosition) Move(Vector3 delta)
174+
public (bool isGrounded, Vector3 deltaPosition, bool wallHit) Move(Vector3 delta)
175175
{
176-
if (!initialPositionAlreadySet) return (true, transform.position);
176+
if (!initialPositionAlreadySet) return (true, transform.position, false);
177177

178178
var previousPosition = transform.position;
179-
characterController.Move(delta);
179+
var collisionFlags = characterController.Move(delta);
180180
Vector3 currentPosition = transform.position;
181181
var deltaPosition = previousPosition - currentPosition;
182182

183183
if (currentPosition.y < 0) { transform.position = new Vector3(currentPosition.x, 0, currentPosition.z); }
184184

185185
ReportPosition(PositionUtils.UnityToWorldPosition(currentPosition));
186186

187-
return (characterController.isGrounded, deltaPosition);
187+
return (characterController.isGrounded, deltaPosition, collisionFlags.HasFlag(CollisionFlags.Sides));
188188
}
189189

190190
public void SetForward(Vector3 forward)
@@ -295,7 +295,7 @@ private float Height(float value) =>
295295

296296
public interface ICharacterView
297297
{
298-
(bool isGrounded, Vector3 deltaPosition) Move(Vector3 delta);
298+
(bool isGrounded, Vector3 deltaPosition, bool wallHit) Move(Vector3 delta);
299299

300300
void SetForward(Vector3 forward);
301301

unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/CharacterControllerV2/DCLCharacterControllerV2.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,14 @@ internal class DCLCharacterControllerV2
5858
private RaycastHit sphereCastHitInfo;
5959
private bool isLongFall;
6060
private Vector3 lastSlopeDelta;
61-
private Vector3 lastFinalVelocity;
61+
private Vector3 lastTargetVelocity; // this is the velocity that we want to move, it always has a value if you are pressing a key to move
62+
private Vector3 lastActualVelocity; // this is the actual velocity that moved the character, its 0 if you are moving against a wall
63+
private float lastImpactMagnitude; // this is 0 if you are at a wall running at it, it gets higher if you run at high velocities and impact the wall
6264
private int groundLayers;
6365
private Ray groundRay;
6466

6567
private CameraMode.ModeId[] tpsCameraModes = new[] { CameraMode.ModeId.ThirdPersonRight, CameraMode.ModeId.ThirdPersonLeft, CameraMode.ModeId.ThirdPersonCenter };
6668
private int currentCameraMode = 0;
67-
private Vector3 lastFinalVelocityBasedOnActualMovement;
6869

6970
public DCLCharacterControllerV2(ICharacterView view, CharacterControllerData data, IInputActionHold jumpAction, IInputActionHold sprintAction, InputAction_Hold walkAction,
7071
IInputActionMeasurable characterXAxis,
@@ -154,17 +155,29 @@ public void Update(float deltaTime)
154155

155156
Vector3 deltaPosition;
156157
bool currentGroundStatus;
158+
bool wallHit;
157159
Vector3 velocityDelta = finalVelocity * deltaTime;
158160

159161
Vector3 downwardsSlopeModifier = GetDownwardsSlopeBasedOnVelocity(velocityDelta);
160162

161-
(currentGroundStatus, deltaPosition) = view.Move(velocityDelta + lastSlopeDelta + downwardsSlopeModifier);
163+
(currentGroundStatus, deltaPosition, wallHit) = view.Move(velocityDelta + lastSlopeDelta + downwardsSlopeModifier);
162164

163-
if (lastFinalVelocity.y >= 0 && finalVelocity.y < 0)
165+
Vector3 currentActualVelocity = lastTargetVelocity.normalized * (Flat(deltaPosition).magnitude / Time.deltaTime);
166+
167+
var currentWallImpactMagnitude = (lastActualVelocity - currentActualVelocity).magnitude;
168+
if (wallHit && currentWallImpactMagnitude < 0.1f && lastImpactMagnitude > 1)
169+
characterState.WallHit();
170+
171+
if (!wallHit)
172+
characterState.ResetWallHit();
173+
174+
lastImpactMagnitude = currentWallImpactMagnitude;
175+
176+
if (lastTargetVelocity.y >= 0 && finalVelocity.y < 0)
164177
lastUngroundPeakHeight = view.GetPosition().y;
165178

166-
lastFinalVelocity = finalVelocity;
167-
lastFinalVelocityBasedOnActualMovement = lastFinalVelocity.normalized * (Flat(deltaPosition).magnitude / Time.deltaTime);
179+
lastTargetVelocity = finalVelocity;
180+
lastActualVelocity = currentActualVelocity;
168181

169182
Vector3 slope = GetSlopeModifier();
170183
lastSlopeDelta = slope * (data.slipSpeedMultiplier * Time.deltaTime);
@@ -198,6 +211,7 @@ public void Update(float deltaTime)
198211

199212
private void OnJustGrounded()
200213
{
214+
characterState.ResetWallHit();
201215
accelerationWeight = 0;
202216

203217
float deltaHeight = lastUngroundPeakHeight - view.GetPosition().y;
@@ -398,7 +412,7 @@ private void CalculateVerticalVelocity(float deltaTime)
398412
if (CanJump())
399413
{
400414
characterState.Jump();
401-
float jumpHeight = GetJumpHeight(Flat(lastFinalVelocityBasedOnActualMovement));
415+
float jumpHeight = GetJumpHeight(Flat(lastActualVelocity));
402416
float jumpStr = Mathf.Sqrt(-2 * jumpHeight * (data.gravity * data.jumpGravityFactor));
403417
velocity.y += jumpStr;
404418
/*var jumpImpulse = new Vector3(velocity.x, jumpStr, velocity.z);

0 commit comments

Comments
 (0)