Uniyt 2D|人物移动脚本

脚本包括:跳跃,左右移动,蓄力跳跃

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

[RequireComponent(typeof(Rigidbody2D))]
[RequireComponent(typeof(Transform))]
[RequireComponent(typeof(BoxCollider2D))]


public class Move : MonoBehaviour
{

    [HeaderAttribute("左右")]
    [TooltipAttribute("移动(开/关)")]
    public bool SpeedOpen;
    [TooltipAttribute("移动速度")]
    public float Speed;

    [HeaderAttribute("跳跃")]
    [TooltipAttribute("跳跃(开/关)")]
    public bool JumpOpen;
    [TooltipAttribute("跳跃高度")]
    public float JumpHeight;
    [TooltipAttribute("支持跳跃的层")]
    public LayerMask JumpLayers;
    [TooltipAttribute("蓄力跳跃(开关)")]
    public bool AccumulateJump;
    [TooltipAttribute("蓄力跳跃(倍数)")]
    public int AccumulateJumpNumber;
    [TooltipAttribute("蓄力跳跃(限制)")]
    public float AccumulateJumpLimit;

    [HeaderAttribute("状态")]
    [SerializeField, TooltipAttribute("跳跃状态")]
    bool JumpState;
    [SerializeField,TooltipAttribute("移动状态")]
    bool SpeedState;

    #region 脚本变量
    Rigidbody2D Rigidbody2D;
    Transform Transform;
    BoxCollider2D BoxCollider2D;

    //跳跃
    bool WOpen;
    float AccumulateJumpTime;
    #endregion

    void Start()
    {
        Rigidbody2D = this.GetComponent<Rigidbody2D>();
        Transform = this.GetComponent<Transform>();
        BoxCollider2D = this.GetComponent<BoxCollider2D>();
    }

    void Update()
    {
        WOpen = Input.GetKey(KeyCode.W);
    }

    void FixedUpdate()
    {
        About();
        Jump();
    }

    #region 左右
    void About()
    {
        float direction;
        direction = Input.GetAxis("Horizontal");
        if (Input.GetAxis("Horizontal") != 0)
        {
            SpeedState = true;
        }
        else
        {
            SpeedState = false;
        }
        Rigidbody2D.velocity = new Vector2(Input.GetAxis("Horizontal") * Speed, Rigidbody2D.velocity.y);
        if(Input.GetAxis("Horizontal") > 0)
        {
            Transform.localScale = new Vector2(1, Transform.localScale.y);
        }
        if (Input.GetAxis("Horizontal") < 0)
        {
            Transform.localScale = new Vector2(-1, Transform.localScale.y);
        }
    }
    #endregion

    #region 跳跃
    //跳跃
    void Jump()
    {
        if (JumpOpen && BoxCollider2D.IsTouchingLayers(JumpLayers)) { 
            if (WOpen)
            {
                if (AccumulateJump)
                {
                    if(AccumulateJumpTime == 0)
                    {
                        AccumulateJumpTime = Time.time;
                    }
                    JumpState = true;
                }
                else
                {
                    Rigidbody2D.AddForce(new Vector2(0,JumpHeight));
                    JumpState = true;
                }
            }
            else
            {
                if (AccumulateJumpTime != 0)
                {
                    float time;
                    if (Time.time - AccumulateJumpTime > AccumulateJumpLimit)
                    {
                        time = AccumulateJumpLimit * AccumulateJumpNumber;
                    }
                    else
                    {
                        time = (Time.time - AccumulateJumpTime) * AccumulateJumpNumber;
                    }
                    Rigidbody2D.AddForce(new Vector2(0, (JumpHeight + time)));
                    Debug.Log(JumpHeight + time);
                    AccumulateJumpTime = 0;
                }
                JumpState = false;
            }
        }
    }
    #endregion
}
0 评论
留言