Ruby's Adventure 03 解决人物碰撞抖动问题

3.

在player controller脚本里面添加代码

public class PlayerController : MonoBehaviour
{
    // Start is called before the first frame update
    //控制人物移动、血量
    public float speed = 100f;
    Rigidbody2D rbody;  //刚体组件
    void Start()
    {
        rbody = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        float moveX = Input.GetAxisRaw("Horizontal");
        //控制水平移动方向 A:-1 D:1 0
        float moveY = Input.GetAxisRaw("Vertical");
        //控制垂直方向 W:1 S:-1 0  //注意规范书写

        Vector2 position = rbody.position;  //玩家自身的位置
        position.x += moveX * Time.deltaTime;
        position.y += moveY * Time.deltaTime;
        //将移动返回给玩家
        //transform.position = position;

        rbody.MovePosition(position);

        //transform.Translate(transform.right * speed * Time.deltaTime);
        //首次测试
    }
}

 

上一篇:Unity射击实例讲解—主角创建


下一篇:Unity相机跟随小结