// Start is called before the first frame update void Start() { _path = GetComponent <MMPathMovement>(); character = GetComponent <Character>(); startingHeight = transform.position.y; beamAnim = Beam.GetComponent <Animator>(); }
public virtual void DetachFromMovingPlatform() { if (_movingPlatform == null) { return; } GravityActive(true); State.OnAMovingPlatform = false; _movingPlatform = null; _movingPlatformCurrentGravity = 0; }
/// <summary> /// On Start, we initialize our path follow if needed /// </summary> protected override void Initialization() { base.Initialization(); _mmPathMovement = this.gameObject.GetComponent <MMPathMovement>(); MovementSpeedMultiplier = 1f; if (AlwaysFollowingPath) { StartFollowingPath(); } }
protected override void Start() { base.Start(); _path = GetComponent <MMPathMovement>(); }
/// <summary> /// On Start we grab our MMPathMovement component /// </summary> protected virtual void Start() { _mmPathMovement = this.gameObject.GetComponent <MMPathMovement>(); }
protected override void Start() { base.Start(); _path = GetComponent <MMPathMovement>(); beamAnim = Beam.GetComponent <Animator>(); }
protected virtual void CastRaysBelow() { State.IsCollidingBelow = true; _friction = 0; if (_newPosition.y < -_smallValue) { State.IsFalling = true; } else { State.IsFalling = false; } if ((Parameters.Gravity > 0) && (!State.IsFalling)) { State.IsCollidingBelow = false; return; } float rayLength = _boundsHeight / 2 + RayOffset; if (State.OnAMovingPlatform) { rayLength *= 2; } if (_newPosition.y < 0) { rayLength += Mathf.Abs(_newPosition.y); } _verticalRayCastFromLeft = (_boundsBottomLeftCorner + _boundsTopLeftCorner) / 2; _verticalRayCastToRight = (_boundsBottomRightCorner + _boundsTopRightCorner) / 2; _verticalRayCastFromLeft += (Vector2)transform.up * RayOffset; _verticalRayCastToRight += (Vector2)transform.up * RayOffset; _verticalRayCastFromLeft += (Vector2)transform.right * _newPosition.x; _verticalRayCastToRight += (Vector2)transform.right * _newPosition.x; if (_belowHitsStorage.Length != NumberOfVerticalRays) { _belowHitsStorage = new RaycastHit2D[NumberOfVerticalRays]; } _raysBelowLayerMaskPlatforms = PlatformMask; _raysBelowLayerMaskPlatformsWithoutOneWay = PlatformMask & ~MidHeightOneWayPlatformMask & ~OneWayPlatformMask & ~MovingOneWayPlatformMask; _raysBelowLayerMaskPlatformsWithoutMidHeight = _raysBelowLayerMaskPlatforms & ~MidHeightOneWayPlatformMask; if (StandingOnLastFrame != null) { _savedBelowLayer = StandingOnLastFrame.layer; if (MidHeightOneWayPlatformMask.Contains(StandingOnLastFrame.layer)) { //StandingOnLastFrame.layer = LayerMask.NameToLayer("Platforms"); } } if (State.WasGroundedLastFrame) { if (StandingOnLastFrame != null) { if (!MidHeightOneWayPlatformMask.Contains(StandingOnLastFrame.layer)) { _raysBelowLayerMaskPlatforms = _raysBelowLayerMaskPlatformsWithoutMidHeight; } } } float smallestDistance = float.MaxValue; int smallestDistanceIndex = 0; bool hitConnected = false; //print("dasdsa2"); for (int i = 0; i < NumberOfVerticalRays; i++) { Vector2 rayOriginPoint = Vector2.Lerp(_verticalRayCastFromLeft, _verticalRayCastToRight, (float)i / (float)(NumberOfVerticalRays - 1)); if ((_newPosition.y > 0) && (!State.WasGroundedLastFrame)) { _belowHitsStorage[i] = MMDebug.RayCast(rayOriginPoint, -transform.up, rayLength, _raysBelowLayerMaskPlatformsWithoutOneWay, Color.blue, Parameters.DrawRaycastsGizmos); } else { _belowHitsStorage[i] = MMDebug.RayCast(rayOriginPoint, -transform.up, rayLength, _raysBelowLayerMaskPlatforms, Color.blue, Parameters.DrawRaycastsGizmos); } float distance = MMMaths.DistanceBetweenPointAndLine(_belowHitsStorage[smallestDistanceIndex].point, _verticalRayCastFromLeft, _verticalRayCastToRight); if (distance < _smallValue) { break; } if (_belowHitsStorage[i]) { if (_belowHitsStorage[i].collider == _ignoredCollider) { continue; } hitConnected = true; State.BelowSlopeAngle = Vector2.Angle(_belowHitsStorage[i].normal, transform.up); _crossBelowSlopeAngle = Vector3.Cross(transform.up, _belowHitsStorage[i].normal); if (_crossBelowSlopeAngle.z < 0) { State.BelowSlopeAngle = -State.BelowSlopeAngle; } if (_belowHitsStorage[i].distance < smallestDistance) { smallestDistanceIndex = i; smallestDistance = _belowHitsStorage[i].distance; } } } //print(hitConnected); if (hitConnected) { StandingOn = _belowHitsStorage[smallestDistanceIndex].collider.gameObject; StandingOnCollider = _belowHitsStorage[smallestDistanceIndex].collider; // if the character is jumping onto a (1-way) platform but not high enough, we do nothing if ( !State.WasGroundedLastFrame && (smallestDistance < _boundsHeight / 2) && ( OneWayPlatformMask.Contains(StandingOn.layer) || MovingOneWayPlatformMask.Contains(StandingOn.layer) ) ) { State.IsCollidingBelow = false; return; } State.IsFalling = false; State.IsCollidingBelow = true; // if we're applying an external force (jumping, jetpack...) we only apply that if (_externalForce.y > 0 && _speed.y > 0) { _newPosition.y = _speed.y * Time.deltaTime; State.IsCollidingBelow = false; } // if not, we just adjust the position based on the raycast hit else { float distance = MMMaths.DistanceBetweenPointAndLine(_belowHitsStorage[smallestDistanceIndex].point, _verticalRayCastFromLeft, _verticalRayCastToRight); _newPosition.y = -distance + _boundsHeight / 2 + RayOffset; } if (!State.WasGroundedLastFrame && _speed.y > 0) { _newPosition.y += _speed.y * Time.deltaTime; } if (Mathf.Abs(_newPosition.y) < _smallValue) { _newPosition.y = 0; } // we check if whatever we're standing on applies a friction change _frictionTest = _belowHitsStorage[smallestDistanceIndex].collider.gameObject.GetComponentNoAlloc <SurfaceModifier>(); if (_frictionTest != null) { _friction = _belowHitsStorage[smallestDistanceIndex].collider.GetComponent <SurfaceModifier>().Friction; } // we check if the character is standing on a moving platform _movingPlatformTest = _belowHitsStorage[smallestDistanceIndex].collider.gameObject.GetComponentNoAlloc <MMPathMovement>(); if (_movingPlatformTest != null && State.IsGrounded) { _movingPlatform = _movingPlatformTest.GetComponent <MMPathMovement>(); } else { DetachFromMovingPlatform(); } } else { State.IsCollidingBelow = true; //State.IsCollidingBelow = false; if (State.OnAMovingPlatform) { DetachFromMovingPlatform(); } } if (StickToSlopes) { StickToSlope(); } }