Unity 2D人物移动实现

Unity 2D人物移动实现

效果展示:
Unity 2D人物移动实现_第1张图片
代码:

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);
        }
    }



}

你可能感兴趣的:(Unity2D,Unity2D人物移动,角色控制,横版游戏)