一、 介绍
知识点:
瓦片地图
如何做到不管从多高落下,回弹速度相同
边缘碰撞器
平台碰撞器
枚举
速度法向量函数collision.contacts[0].normal
Lerp函数
SmoothDamp函数
动画制作
对象池
二、 平台
两种平台,第一种跳跃一次就销毁(播放动画)
第二种可以一直跳跃
using System. Collections ;
using System. Collections. Generic ;
using UnityEngine ;
public class Platform : MonoBehaviour
{
public PlatformType platformType;
public float bounceSpeed = 4f ;
private void OnCollisionEnter2D ( Collision2D collision)
{
if ( collision. contacts[ 0 ] . normal == Vector2. down)
{
Rigidbody2D rb = collision. gameObject. GetComponent < Rigidbody2D> ( ) ;
if ( rb != null )
{
rb. velocity = Vector2. up * bounceSpeed;
}
if ( platformType == PlatformType. weak)
{
if ( GetComponent < Animator> ( ) != null )
{
GetComponent < Animator> ( ) . SetTrigger ( "Trigger" ) ;
Invoke ( "HideGameObject" , 0.4f ) ;
}
}
}
}
private void OnTriggerExit2D ( Collider2D collision)
{
if ( collision. CompareTag ( "MainCamera" ) )
{
gameObject. SetActive ( false ) ;
}
}
void HideGameObject ( )
{
gameObject. SetActive ( false ) ;
}
}
public enum PlatformType
{
normal, weak
}
}
三、玩家
左右控制行走、转向
using System. Collections ;
using System. Collections. Generic ;
using UnityEngine ;
public class Doodler : MonoBehaviour
{
public float moveSpeed;
Rigidbody2D rb;
void Start ( )
{
rb = GetComponent < Rigidbody2D> ( ) ;
}
void Update ( )
{
float h = Input. GetAxisRaw ( "Horizontal" ) ;
rb. velocity = new Vector2 ( h * moveSpeed, rb. velocity. y) ;
if ( h != 0 )
{
transform. localScale = new Vector3 ( - h, 1 , 1 ) ;
}
}
}
四、 换边儿
左边进去,右边出来。
右边进去,左边出来
using System. Collections ;
using System. Collections. Generic ;
using UnityEngine ;
public class ChangeSide : MonoBehaviour
{
private void OnTriggerExit2D ( Collider2D collision)
{
Transform t = collision. gameObject. transform;
t. position = new Vector3 ( ( - t. position. x) / 0.95f , t. position. y, 0f ) ;
}
}
五、 相机视角
相机跟随玩家移动,使用SmoothDamp函数
摄像机添加碰撞体
using System. Collections ;
using System. Collections. Generic ;
using UnityEngine ;
public class CameraFollow : MonoBehaviour
{
public Transform target;
public float smoothSpeed = 0.3f ;
Vector3 speed;
private void LateUpdate ( )
{
if ( target. position. y > transform. position. y)
{
Vector3 targetPos = new Vector3 ( 0f , target. position. y, - 10f ) ;
transform. position = Vector3. SmoothDamp ( transform. position, targetPos, ref speed, smoothSpeed * Time. deltaTime) ;
}
}
}
六、 对象池
生成挡板,防止频繁销毁创建、销毁占用系统资源
using System. Collections ;
using System. Collections. Generic ;
using UnityEngine ;
public class LevelGenerator : MonoBehaviour
{
public GameObject[ ] platformPrefabs;
float currentYPos = 0f ;
public float cameraHeight = 5.5f ;
public Transform platformPool;
void Start ( )
{
SpawnPlatformPool ( ) ;
while ( currentYPos < Camera. main. transform. position. y + cameraHeight)
{
PickNewPlatform ( ) ;
}
}
void SpawnPlatformPool ( )
{
int basicPlatformAmount = 30 ;
int weakPlatformAmount = 15 ;
for ( int i = 0 ; i < basicPlatformAmount; i++ )
{
GameObject platform = Instantiate ( platformPrefabs[ 0 ] , platformPool) ;
platform. SetActive ( false ) ;
}
for ( int i = 0 ; i < weakPlatformAmount; i++ )
{
GameObject platform = Instantiate ( platformPrefabs[ 1 ] , platformPool) ;
platform. SetActive ( false ) ;
}
}
void PickNewPlatform ( )
{
currentYPos += Random. Range ( 0.3f , 1f ) ;
float xPos = Random. Range ( - 3.8f , 3.8f ) ;
int r = 0 ;
do
{
r = Random. Range ( 0 , platformPool. childCount) ;
} while ( platformPool. GetChild ( r) . gameObject. activeInHierarchy) ;
platformPool. GetChild ( r) . position = new Vector2 ( xPos, currentYPos) ;
platformPool. GetChild ( r) . gameObject. SetActive ( true ) ;
}
void Update ( )
{
if ( currentYPos < Camera. main. transform. position. y + cameraHeight)
{
PickNewPlatform ( ) ;
}
}
}
六、 下载工程文件
https://wwez.lanzoul.com/iUBpf0tdo7ih