1.导入并处理素材
将网上下载好的素材拖入Assets文件夹中。
将Sprite Mode由single改成Multiple,点击Sprite Editor按钮,在图上将所需要图案划分成一个个sprite,点击apply,完成图案裁剪。
2.完成简单布局
3.实现人物移动和跳跃
给人物和平台身上附加Box Collider 2D碰撞检测箱,调整至合适大小。点击Edit Collider按钮可随意更改Box形状。
给人物添加Rigidbody 2D刚体,Gravity Scale调整重力大小。若在后续添加移动脚本,不想人物进行旋转,记得点击Freeze Rotation Z按钮,用于锁定人物旋转方向。
添加移动脚本MoveController,根据左右按键进行移动,空格进行跳跃。在进行跳跃时,新建一个bool变量isground,用于判断是否与地面接触。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveController : MonoBehaviour
{
public GameObject Player;
public float m_speed = 5f;
private Rigidbody2D rb;
private Collider2D coll;
public float jumpspeed = 8f;
float xVelocity;
public bool isOnGround;
public LayerMask groundLayer;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
coll = GetComponent<Collider2D>();
}
// Update is called once per frame
void FixedUpdate()
{
PlayerMove_KeyTransform();
}
void isOnGroundCheck()
{
//判断角色碰撞器与地面图层发生接触
if (coll.IsTouchingLayers(groundLayer))
{
isOnGround = true;
}
else
{
isOnGround = false;
}
}
void Jump()
{
rb.velocity = new Vector2(rb.velocity.x, jumpspeed);
}
public void PlayerMove_KeyTransform()
{
isOnGroundCheck();
if (Input.GetKey(KeyCode.A) | Input.GetKey(KeyCode.LeftArrow)) //左
{
Player.transform.Translate(Vector3.right * -m_speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D) | Input.GetKey(KeyCode.RightArrow)) //右
{
Player.transform.Translate(Vector3.right * m_speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.Space))
{
if (isOnGround == true)
{
Jump();
}
}
}
}
4.实现镜头跟随
实现镜头跟随,相当于保持一定的z轴距离,将镜头x,y坐标移到被跟随物体上。为了使镜头移动更加平滑,添加smoothing,使用Vector3.Lerp(a,b,smoothing*Time.fixedDeltatime)函数。该函数相当于将b-a的距离分成多个部分,逐帧移动。添加一个ylimit值,使得人物在掉落平台时,镜头不会跟随人物落到没有背景的地方。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollower : MonoBehaviour
{
public Transform target;
public float smoothing;
public float ylimit;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void LateUpdate()
{
if (target != null)
{
Vector3 targetPosition = new Vector3(target.position.x, target.position.y / smoothing * Time.fixedDeltaTime, transform.position.z);
if (target.position.y < ylimit)
{
targetPosition = new Vector3(target.position.x, ylimit / smoothing * Time.fixedDeltaTime, transform.position.z);
}
Vector3 smoothedPosition = Vector3.Lerp(transform.position, targetPosition, smoothing * Time.fixedDeltaTime);
transform.position = smoothedPosition;
}
}
}
5.实现前后景快慢移动
人物在移动过程中,前景实际应该移动最快,其次是平台,最后是背景。但在普通相机下移动速率都一致。所以新建一个Empty,命名为GM,用于控制不同背景。在scripts新建一个Parallaxing的C#代码,挂载在GM上。在Parallaxing中创建一个Transform矩阵,用于添加需管理的背景。计算出每个背景离摄像机的z距离,作为平行移动倍数,每次摄像机移动时,将移动距离之差乘以移动倍数,重新赋给背景,使背景进行一定的位移,达成近快远慢的效果。
using UnityEngine;
using System.Collections;
public class Parallaxing : MonoBehaviour {
public Transform[] backgrounds; // Array (list) of all the back- and foregrounds to be parallaxed
private float[] parallaxScales; // The proportion of the camera's movement to move the backgrounds by
public float smoothing = 1f; // How smooth the parallax is going to be. Make sure to set this above 0
private Transform cam; // reference to the main cameras transform
private Vector3 previousCamPos; // the position of the camera in the previous frame
// Is called before Start(). Great for references.
void Awake () {
// set up camera the reference
cam = Camera.main.transform;
}
// Use this for initialization
void Start () {
// The previous frame had the current frame's camera position
previousCamPos = cam.position;
// asigning coresponding parallaxScales
parallaxScales = new float[backgrounds.Length];
for (int i = 0; i < backgrounds.Length; i++) {
parallaxScales[i] = backgrounds[i].position.z*-1;
}
}
// Update is called once per frame
void Update () {
// for each background
for (int i = 0; i < backgrounds.Length; i++) {
// the parallax is the opposite of the camera movement because the previous frame multiplied by the scale
float parallax = (previousCamPos.x - cam.position.x) * parallaxScales[i];
// set a target x position which is the current position plus the parallax
float backgroundTargetPosX = backgrounds[i].position.x + parallax;
// create a target position which is the background's current position with it's target x position
Vector3 backgroundTargetPos = new Vector3 (backgroundTargetPosX, backgrounds[i].position.y, backgrounds[i].position.z);
// fade between current position and the target position using lerp
backgrounds[i].position = Vector3.Lerp (backgrounds[i].position, backgroundTargetPos, smoothing * Time.deltaTime);
}
// set the previousCamPos to the camera's position at the end of the frame
previousCamPos = cam.position;
}
}