花1.png

类吸血鬼实战(1):精灵图使用、人物移动、地图制作

概要:类吸血鬼实战(1):精灵图使用、人物移动、地图制作

第一课:精灵图介绍使用

第二课:2D中人物移动

public class Player : MonoBehaviour
{
    // Start is called before the first frame update
    public Vector2 inputVec;
    public float speed = 5;
    Rigidbody2D rigid;
    void Start()
    {
        rigid = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        inputVec.x = Input.GetAxisRaw("Horizontal");
        inputVec.y = Input.GetAxisRaw("Vertical");
    }
    void FixedUpdate()
    {
        // 1.力控制
        // rigid.AddForce(inputVec);
        // 2.速度控制
        // rigid.velocity = inputVec;
        // 3.位置控制
        Vector2 nextVec = inputVec.normalized * speed * Time.fixedDeltaTime;
        rigid.MovePosition(rigid.position + nextVec);
    }
}

第三课:使用Input System移动控制

public class Player : MonoBehaviour
{
    // Start is called before the first frame update
    public Vector2 inputVec;
    public float speed = 5;
    Rigidbody2D rigid;
    void Start()
    {
        rigid = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        // 1.力控制
        // rigid.AddForce(inputVec);
        // 2.速度控制
        // rigid.velocity = inputVec;
        // 3.位置控制
        Vector2 nextVec = inputVec.normalized * speed * Time.fixedDeltaTime;
        rigid.MovePosition(rigid.position + nextVec);
    }
    void OnMove(InputValue value)
    {
        inputVec = value.Get<Vector2>();
    }
}

第四课:移动动画

人物翻转

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class Player : MonoBehaviour
{
    /*.....*/
    SpriteRenderer spriteRenderer;
    /*.....*/
    void LateUpdate()
    {
        if(inputVec.x != 0) {
            spriteRenderer.flipX = inputVec.x < 0;
        }
    }
}

人物移动(动画状态机)

动画状态机

C#变量控制动画人物反转

void LateUpdate()
{
    animator.SetFloat("Speed", inputVec.magnitude);
    if (inputVec.x != 0)
    {
        spriteRenderer.flipX = inputVec.x < 0;
    }
}

Animator Override Controller组件

结构相同的动画进行使用

第五课:地图背景制作

Tile使用

画地图

创建GameManager、Reposition

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
// 当玩家到达地图边缘移动地图达到无限地图
public class Reposition : MonoBehaviour
{
    void OnTriggerExit2D(Collider2D collider)
    {
        if (!collider.CompareTag("Area"))
        {
            return;
        }
        Vector3 playerPos = GameManager.instance.player.transform.position;
        Vector3 myPos = transform.position;
        float diffx = Mathf.Abs(playerPos.x - myPos.x);
        float diffy = Mathf.Abs(playerPos.y - myPos.y);

        Vector3 playerDir = GameManager.instance.player.inputVec;
        float dirX = playerDir.x < 0 ? -1 : 1;
        float dirY = playerDir.y < 0 ? -1 : 1;
        switch (transform.tag)
        {
            case "Ground":
                if (diffx > diffy)
                {
                    transform.Translate(Vector3.right * dirX * 40);
                }
                else if (diffx < diffy)
                {
                    transform.Translate(Vector3.up * dirY * 40);
                }
                break;
            case "Enemy":

                break;
        }
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour
{
    public static GameManager instance;
    public Player player;

    void Awake()
    {
        instance = this;
    }

}

设置虚拟相机跟随玩家

Created By @Seeyou | 稀有博客