Merge pull request #6 from Olezen/Ladders

Ladders
This commit is contained in:
Olezen
2019-11-10 12:33:49 +01:00
committed by GitHub
5 changed files with 105 additions and 1 deletions

View File

@@ -0,0 +1,10 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ladder : MonoBehaviour {
// Empty class for defining objects as ladders
// Probably inefficient, but I didn't want to use tags or layers
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3f4efb5e88333de4e8a64c4b9da57961
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -42,6 +42,12 @@ namespace Fragsurf.Movement {
public bool toggleCrouch = false;
public bool slidingEnabled = false;
public bool laddersEnabled = false;
public bool climbingLadder = false;
public Vector3 ladderNormal = Vector3.zero;
public Vector3 ladderDirection = Vector3.forward;
public Vector3 ladderVelocity = Vector3.zero;
public bool underwater = false;
public bool cameraUnderwater = false;

View File

@@ -38,6 +38,7 @@ namespace Fragsurf.Movement {
[Header ("Features")]
public bool crouchingEnabled = true;
public bool slidingEnabled = false;
public bool laddersEnabled = true;
[Header ("Step offset (can be buggy, enable at your own risk)")]
public bool useStepOffset = false;
@@ -194,6 +195,7 @@ namespace Fragsurf.Movement {
_moveData.rigidbodyPushForce = rigidbodyPushForce;
_moveData.slidingEnabled = slidingEnabled;
_moveData.laddersEnabled = laddersEnabled;
_moveData.playerTransform = transform;
_moveData.viewTransform = viewTransform;

View File

@@ -44,7 +44,18 @@ namespace Fragsurf.Movement {
_config = config;
_deltaTime = deltaTime;
if (!_surfer.moveData.underwater) {
if (_surfer.moveData.laddersEnabled && !_surfer.moveData.climbingLadder) {
// Look for ladders
LadderCheck (new Vector3(1f, 0.5f, 1f), _surfer.moveData.velocity * Time.deltaTime * 2f);
}
if (_surfer.moveData.laddersEnabled && _surfer.moveData.climbingLadder) {
LadderPhysics ();
} else if (!_surfer.moveData.underwater) {
if (_surfer.moveData.velocity.y <= 0f)
jumping = false;
@@ -283,7 +294,71 @@ namespace Fragsurf.Movement {
_surfer.moveData.velocity.y = Mathf.Max (_surfer.moveData.velocity.y, _config.jumpForce);
}
private void LadderCheck (Vector3 colliderScale, Vector3 direction) {
if (_surfer.moveData.velocity.sqrMagnitude <= 0f)
return;
Debug.Log ("Checking for ladders");
bool foundLadder = false;
RaycastHit [] hits = Physics.BoxCastAll (_surfer.moveData.origin, Vector3.Scale (_surfer.collider.bounds.size * 0.5f, colliderScale), Vector3.Scale (direction, new Vector3 (1f, 0f, 1f)), Quaternion.identity, direction.magnitude, SurfPhysics.groundLayerMask, QueryTriggerInteraction.Collide);
foreach (RaycastHit hit in hits) {
Ladder ladder = hit.transform.GetComponentInParent<Ladder> ();
if (ladder != null) {
foundLadder = true;
if (_surfer.moveData.climbingLadder == false) {
_surfer.moveData.climbingLadder = true;
_surfer.moveData.ladderNormal = hit.normal;
_surfer.moveData.ladderDirection = direction;
}
}
}
if (!foundLadder) {
_surfer.moveData.ladderNormal = Vector3.zero;
_surfer.moveData.ladderVelocity = Vector3.zero;
_surfer.moveData.climbingLadder = false;
}
}
private void LadderPhysics () {
_surfer.moveData.ladderVelocity = Vector3.up * _surfer.moveData.verticalAxis * 6f;
_surfer.moveData.velocity = Vector3.Lerp (_surfer.moveData.velocity, _surfer.moveData.ladderVelocity, Time.deltaTime * 10f);
LadderCheck (Vector3.one, _surfer.moveData.ladderDirection);
Trace floorTrace = TraceToFloor ();
if (_surfer.moveData.verticalAxis < 0f && floorTrace.hitCollider != null) {
_surfer.moveData.velocity = _surfer.moveData.ladderNormal * 0.5f;
_surfer.moveData.ladderVelocity = Vector3.zero;
_surfer.moveData.climbingLadder = false;
}
if (_surfer.moveData.wishJump) {
_surfer.moveData.velocity = _surfer.moveData.ladderNormal * 4f;
_surfer.moveData.ladderVelocity = Vector3.zero;
_surfer.moveData.climbingLadder = false;
}
}
private void Accelerate (Vector3 wishDir, float wishSpeed, float acceleration, bool yMovement) {
// Initialise variables