Unity 2D人物移动实现

Unity 2D人物移动实现

效果展示:
在这里插入图片描述
代码:

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

public class ParentnewController00 : MonoBehaviour
{
    private Rigidbody2D rg;
    private Collider2D coll;
    private Animator anim;

    public GameObject lift;//托举物品
    public float dis = 0;

    //托举
    public float hight = 3;
    public Transform kid_transform;
    public Transform box_transform;

    public float moveSpeed, jumpForce;

    public Transform groundcheck;
    public LayerMask ground;

    public bool isGround, isJump;

    public bool jumpPressed;
    int jumpCount;

    float h = 0;

    bool walk = true;

    float speed = 0;

    bool ispulling = false;
    void Start()
    {
        rg = GetComponent<Rigidbody2D>();
        coll = GetComponent<Collider2D>();
        anim = GetComponent<Animator>();
        lift.SetActive(false);
        speed = moveSpeed;
    }

    // Update is called once per frame
    void Update()
    {
        // Debug.Log(rg.velocity.y);

        if (Input.GetKeyDown(KeyCode.W) && jumpCount > 0)
        {
            Debug.Log("按下了跳跃键");
            jumpPressed = true;
        }
        if (Input.GetKeyUp(KeyCode.W) && jumpCount > 0)
        {
            Debug.Log("松开跳跃键");
            jumpPressed = false;
        }

        if (Input.GetKey(KeyCode.S))
        {
            anim.SetBool("down", true);
            walk = false;
            if (kid_transform.position.y - box_transform.position.y > hight)
                lift.SetActive(true);
        }
        else
        {
            anim.SetBool("down", false);
            walk = true;
            lift.SetActive(false);
        }


    }

    private void FixedUpdate()
    {

        isGround = coll.IsTouchingLayers(ground);
        groundMovement();
        Jump();
        switchJump();
    }

    void groundMovement()
    {

        if (walk)
        {
            if (Input.GetButton("A"))
            {
                h = -1;
                anim.SetBool("running", true);
                //if (Input.GetKey(KeyCode.LeftShift))
                //{
                //    moveSpeed = 4;
                //}
                //else
                //{
                //    moveSpeed = speed;
                //}

            }
            else if (Input.GetButton("D"))
            {
                h = 1;
                anim.SetBool("running", true);
                //if (Input.GetKey(KeyCode.LeftShift))
                //{
                //    moveSpeed = 4;
                //}
                //else
                //{
                //    moveSpeed = speed;
                //}
            }
            else
            {
                h = 0;
                anim.SetBool("running", false);
            }
        }

        //rg.velocity = new Vector2(h * moveSpeed, rg.velocity.y);

        transform.Translate(Vector3.right * h * moveSpeed * Time.fixedDeltaTime);

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

    void Jump()
    {
        if (isGround)
        {
            jumpCount = 1;//设置跳跃的次数
            isJump = false;
        }
        if (jumpPressed && isGround)
        {
            isJump = true;
            rg.velocity = new Vector2(rg.velocity.x, jumpForce);
            jumpCount--;
            jumpPressed = false;
        }
        else if (jumpPressed && jumpCount > 0 && isJump)
        {
            rg.velocity = new Vector2(rg.velocity.x, jumpForce);
            jumpCount--;
            jumpPressed = false;
        }

    }

    void switchJump()
    {
        if (isGround)
        {
            anim.SetBool("jump", false);
        }
        else if (!isGround && rg.velocity.y > 0)
        {
            anim.SetBool("jump", true);
        }
        else if (rg.velocity.y < 0 && !isGround)
        {
            anim.SetBool("jump", true);
        }
    }



}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值