Unity 2D人物移动实现

Posted Z_hongli

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
        
    





以上是关于Unity 2D人物移动实现的主要内容,如果未能解决你的问题,请参考以下文章

Unity 2D人物移动实现

时光煮雨 Unity3D实现2D人物移动-总结篇

[Unity3D/2D]实现相机对人物角色的跟随效果/相机在一定范围内移动/内置插件实现

unity2d用上下左右移动人物的情况下,怎么把Y轴移动的值赋给Z轴(比如Y改变5,Z跟着改变8)

Unity游戏开发精品素材资源——2D图标素材篇(头像、装备与物品、技能)

时光煮雨 Unity3d 序列目标点的移动①