横版2D小游戏开局_学习整理记录

核心1、新建工程,取名Exercise_2D。
2、新建个文件夹(2D_Assets),将所有素材资源,导入或拖入。
3、修改图片像素为16或32,或适合人物的尺寸。
4、新建个空物体,取名Back,然后 直接拖入背景图片 作为子物体。然后在Back父物体上添加Collider。
5、调整 相机的Size,使能有一个较开阔的视野。
6、新建 Sprites并取名Player,然后 将第一帧的人物图片拖入sprites。然后添加 Animator组件。
7、新建文件夹(Animation_Player),新建Animator(取名Player),将其拖入Player物体里的Animator组件。
8、选中Player,在Animation窗口,点击Create新建Animation。(取名:idle、run、jump)
9、将 各个人物帧图片拖入idle的Animation窗口,调整Samples频率。
10、对 Player添加Rigidbody2D刚体,和Collider2D碰撞器组件。然后 锁定刚体的Z轴和调整碰撞器。
11、对 Player添加脚本(PlayerController)。
       1.获取private Rigibody2D、float speed;
       2.Start里:rb2D=GetComponent<Rigibody2D>();
       3.新建方法PlayerMove(); 里面写 水平移动的代码、朝向代码、动画播放。***(移动和跳跃代码放FixedUpdate里)
         3.1.动画播放:做切换动画的条件,并 写播放动画的代码
12、在Player脚本里,新建方法playerJump(); 里面写 跳跃代码、动画播放。
       1.创建 bool值(jumpDown,表示是否按下跳跃键),创建float值(jumpForce,表示跳跃力)
       2.创建3个if方法,表示:跳跃力、起跳动画、下落动画。
13、对 摄像机添加脚本(CameraController)。
       1.获取 Player的Transform。
       2.thansform.position只有x轴跟随玩家
******(目前来说,简单的移动就完成了,没有判断是否接触地面,所以现在可以无限跳跃)******

//面试遇到的需求:

1、点击屏幕左右侧,控制人物移动
     1.通过 从相机发出的射线来判断
     2.核心代码:RaycastHit2D hit = Physics2D.Raycast();//获取射线碰撞到的物体
     3.判断碰撞到的物体是什么标签tag。
     4.新建2个Sprites,分别放在屏幕左右两侧,并标签:LeftMove,RightMove

/// <summary>
/// 玩家控制器
/// </summary>
public class PlayerController : MonoBehaviour
{
    private Rigidbody2D rb2D;
    public float speed;//移动速度
    private Animator anim;

    public bool jumpDown;
    public float jumpForce;


    private void Start()
    {
        rb2D = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
    }


    private void Update()
    {
        if (Input.GetButtonDown("Jump"))
        {
            jumpDown = true;
        }

        if (Input.GetMouseButtonUp(0))
        {
            //抬起 鼠标时,要停止移动
            rb2D.velocity = new Vector2(0, rb2D.velocity.y);

            //Debug.Log("AAA");
        }
    }

    private void FixedUpdate()
    {
        PlayerMove();//正常键盘控制
        PlayerJump();

        MouseMove();//点击屏幕来 控制角色
    }

    //点击屏幕来 移动角色
    public void MouseMove()
    {
        if (Input.GetMouseButton(0))
        {
            RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);

            if (hit.collider != null)
            {
                switch (hit.collider.gameObject.tag)
                {
                    case "Player":
                        rb2D.velocity = new Vector2(rb2D.velocity.x, jumpForce);
                        break;
                    case "LeftMove":
                        rb2D.velocity = new Vector2(-speed, rb2D.velocity.y);
                        transform.localScale = new Vector3(-1, 1, 1);
                        break;
                    case "RightMove":
                        rb2D.velocity = new Vector2(speed, rb2D.velocity.y);
                        transform.localScale = new Vector3(1, 1, 1);
                        break;
                }
            }
        }

        anim.SetFloat("Run", Mathf.Abs(rb2D.velocity.x));

        if (rb2D.velocity.y > 0)
        {
            anim.SetBool("Jump", true);
        }

        if (rb2D.velocity.y < 0)
        {
            anim.SetBool("Jump", false);
        }
    }

    //玩家水平移动(键盘)
    public void PlayerMove()
    {
        float _horizontal = Input.GetAxisRaw("Horizontal");

        rb2D.velocity = new Vector2(_horizontal * speed, rb2D.velocity.y);

        if (_horizontal != 0)
        {
            transform.localScale = new Vector3(_horizontal, 1, 1);
        }

        anim.SetFloat("Run", Mathf.Abs(_horizontal));
    }

    //玩家跳跃(键盘)
    public void PlayerJump()
    {
        if (jumpDown)
        {
            rb2D.velocity = new Vector2(rb2D.velocity.x, jumpForce);
            jumpDown = false;
        }

        if (rb2D.velocity.y > 0)
        {
            anim.SetBool("Jump", true);
        }

        if (rb2D.velocity.y < 0)
        {
            anim.SetBool("Jump", false);
        }

    }

}
/// <summary>
/// 相机控制器
/// </summary>
public class CameraController : MonoBehaviour
{
    public Transform player;


    private void Update()
    {
        transform.position = new Vector3(player.position.x, transform.position.y, transform.position.z);
    }

}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值