unity2D小游戏rubyadventure练习记录


  • 人物被背景遮挡:更改sorting layer,值越大越靠前

    https://blog.csdn.net/ithot/article/details/90679992
  • 给物体添加碰撞体(Collider),给人物添加碰撞体和刚体(Rigidbody,物理引擎)
    刚体一定要绑定在被碰撞的对象上才能产生碰撞效果,而碰撞体则不一定要绑定刚体。
    2D情况下刚体重力设为1否则会往下掉
    http://c.biancheng.net/view/2749.html
    加了物理的模型可以单独保存然后批量复制,对本体修改后apply,所有clone体也会修改

    2D的覆盖顺序要以y轴排序
    防止碰撞旋转:禁用z轴

    解决碰撞抖动:直接修改刚体位置(改过之后同样speed下移动变慢不知道为啥)
//移动代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class control : MonoBehaviour
{
    public float speed = 5f;

    Rigidbody2D rbody;
    // Start is called before the first frame update
    void Start()
    {
        rbody = GetComponent();
    }

    // Update is called once per frame
    void Update()
    {
        //transform.Translate(transform.right * speed * Time.deltaTime);
        float movex = Input.GetAxisRaw("Horizontal");
        float movey = Input.GetAxisRaw("Vertical");

        Vector2 position = transform.position;
        position.x += movex * speed * Time.deltaTime;
        position.y += movey * speed * Time.deltaTime;
        //transform.position = position;
        rbody.MovePosition(position);
    }
}