using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class myMove : MonoBehaviour
{
private Rigidbody2D rb;
private BoxCollider2D coll;
private float ability = 1;//角色能力属性,更改其大小可以改变速度等
public float speed = 1f;
private bool if_Jump = true;//判断物体是否悬空,悬空则为false
private float yposition;//记录物体上一次的y轴坐标
void Start()
{
rb = GetComponent<Rigidbody2D>();
coll = GetComponent<BoxCollider2D>();
Vector2 position = transform.position;
yposition = position.y;
}
private void FixedUpdate()
{
float horizintal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Jump");
Vector2 position = transform.position;
position.x = position.x + speed * ability * horizintal;
if (Input.GetButton("Jump") && if_Jump)
{
position.y = position.y + speed * ability * vertical;
if_Jump = false;
}
//判断是否有空格键且是否处于悬空状态
if (yposition == position.y)
{
if_Jump = true;
}
//判断是否落地
yposition = position.y;
transform.position = position;
}
}
以上为实现角色2D平台跳跃脚本