1.Destroyer
using UnityEngine;
public class Destroyer : MonoBehaviour
{
[ SerializeField] private float lifeTimer;
private void Start ( )
{
Destroy ( gameObject, lifeTimer) ;
}
}
2.Echo
using UnityEngine;
public class Echo : MonoBehaviour
{
private Jump jump;
public GameObject echo;
private float timeBtSpawn;
[ SerializeField] private float spawnRate;
private void Start ( )
{
jump = GetComponent< Jump> ( ) ;
}
private void Update ( )
{
if ( jump. GetComponent< Rigidbody2D> ( ) . velocity. x != 0 )
{
if ( timeBtSpawn >= spawnRate)
{
if ( jump. moveH > 0 )
{
GameObject instance = Instantiate ( echo, transform. position, Quaternion. identity) ;
Destroy ( instance, 2.0f ) ;
timeBtSpawn = 0 ;
}
if ( jump. moveH < 0 )
{
GameObject instance = Instantiate ( echo, transform. position, Quaternion. identity) ;
instance. transform. eulerAngles = new Vector3 ( 0 , 180 , 0 ) ;
Destroy ( instance, 2.0f ) ;
timeBtSpawn = 0 ;
}
}
else
{
timeBtSpawn + = Time. deltaTime;
}
}
}
}
3.Jump
using UnityEngine;
[ RequireComponent ( typeof ( Rigidbody2D) ) ]
public class Jump : MonoBehaviour
{
private Rigidbody2D rb;
[ SerializeField] private float jumpForce;
[ HideInInspector] public float moveH;
[ SerializeField] private float moveSpeed;
private SpriteRenderer sp;
[ SerializeField] private bool isGround;
public Transform checkPoint;
[ SerializeField] private float checkRadius;
public LayerMask layerMask;
[ SerializeField] private Vector2 checkBoxSize;
[ Header ( "Better Jump" ) ]
[ SerializeField]
private float fallFactor;
[ SerializeField] private float shortJumpFactor;
[ Header ( "Second Jump" ) ] [ SerializeField]
private float jumpTimes;
[ Header ( "VFX" ) ] [ SerializeField] private GameObject jumpEffect;
private void Start ( )
{
rb = GetComponent< Rigidbody2D> ( ) ;
sp = GetComponent< SpriteRenderer> ( ) ;
Physics2D. queriesStartInColliders = false;
}
private void Update ( )
{
if ( Input. GetKeyDown ( KeyCode. Space) && isGround)
{
rb. velocity = Vector2. up * jumpForce;
Instantiate ( jumpEffect, transform. position - Vector3. up, Quaternion. identity) ;
}
moveH = Input. GetAxis ( "Horizontal" ) * moveSpeed;
rb. velocity = new Vector2 ( moveH, rb. velocity. y) ;
Flip ( ) ;
CheckGround ( ) ;
BetterJump ( ) ;
}
private void CheckGround ( )
{
#region Raycast Method has Disadvantage
#endregion
Collider2D collider = Physics2D. OverlapBox ( checkPoint. position, checkBoxSize, 0 , layerMask) ;
if ( collider != null)
{
isGround = true;
}
else
{
isGround = false;
}
}
private void OnDrawGizmos ( )
{
Gizmos. DrawWireCube ( checkPoint. position, checkBoxSize) ;
Gizmos. color = Color. green;
}
private void Flip ( )
{
if ( moveH > 0 )
{
transform. eulerAngles = new Vector3 ( 0 , 0 , 0 ) ;
}
else if ( moveH < 0 )
{
transform. eulerAngles = new Vector3 ( 0 , 180 , 0 ) ;
}
}
private void BetterJump ( )
{
if ( rb. velocity. y < 0 )
{
rb. velocity + = Vector2. up * Physics2D. gravity. y * fallFactor * Time. deltaTime;
}
else if ( rb. velocity. y > 0 && ! Input. GetKey ( KeyCode. Space) )
{
rb. velocity + = Vector2. up * Physics2D. gravity. y * shortJumpFactor * Time. deltaTime;
}
}
}
4.Light
using UnityEngine;
public class Light : MonoBehaviour
{
[ SerializeField] private float rotateSpeed;
private void Update ( )
{
transform. Rotate ( 0 , 0 , rotateSpeed * Time. deltaTime) ;
}
}
5.OneWayPlatform
using UnityEngine;
public class OneWayPlatform : MonoBehaviour
{
private PlatformEffector2D platformEffect2D;
public float holdTime;
private void Start ( )
{
platformEffect2D = GetComponent< PlatformEffector2D> ( ) ;
}
private void Update ( )
{
if ( Input. GetKeyUp ( KeyCode. S) )
{
holdTime = 0.5f ;
}
if ( Input. GetKey ( KeyCode. S) )
{
if ( holdTime <= 0 )
{
platformEffect2D. rotationalOffset = 180f ;
holdTime = 0.5f ;
}
else
{
holdTime - = Time. deltaTime;
}
}
if ( Input. GetKey ( KeyCode. Space) )
{
platformEffect2D. rotationalOffset = 0f ;
}
}
}