unity-物体的基本操作笔记

unity-物体的基本操作笔记

    • 公共属性
    • 左右移动

公共属性

    private Rigidbody2D m_body2d;
    [SerializeField] float m_speed = 4.0f;
    [SerializeField] float m_jumpForce = 7.5f;

左右移动

    // Update is called once per frame
    void Update()
    {
        float inputX = Input.GetAxis("Horizontal");
		//切换方向
        if (inputX > 0)
            transform.localScale = new Vector3(-1.0f, 1.0f, 1.0f);
        else if (inputX < 0)
            transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
		//移动
        m_body2d.velocity = new Vector2(inputX * m_speed, m_body2d.velocity.y);
    }

    void Update()
    {
        if (Input.GetKeyDown("space"))
        {
            m_body2d.velocity = new Vector2(m_body2d.velocity.x, m_jumpForce);
        }

你可能感兴趣的:(unity,unity2d,unity,笔记,java)