本文介绍了unity 2d 跳跃脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人有一个很好的 2d 游戏的跳跃脚本?我的代码有效,但还远没有跳起来,看起来像是在飞.

Does anyone have a good jumping script for 2d games in unity? The code I have works but still is far from jumping, it looks like it is flying.

using UnityEngine;
using System.Collections;

public class movingplayer : MonoBehaviour {

public Vector2 speed = new Vector2(10,10);

private Vector2 movement = new Vector2(1,1);

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
    float inputX = Input.GetAxis ("Horizontal");
    float inputY = Input.GetAxis ("Vertical");

    movement = new Vector2(
        speed.x * inputX,
        speed.y * inputY);

    if (Input.GetKeyDown ("space")){
                         transform.Translate(Vector3.up * 260 * Time.deltaTime, Space.World);
                 }

}
void FixedUpdate()
{
    // 5 - Move the game object
    rigidbody2D.velocity = movement;
    //rigidbody2D.AddForce(movement);

}
}

推荐答案

通常用于跳跃的人使用 Rigidbody2D.AddForceForcemode.Impulse.看起来你的物体在 Y 轴上被推动一次,它会由于重力而自动落下.

Usually for jumping people use Rigidbody2D.AddForce with Forcemode.Impulse. It may seem like your object is pushed once in Y axis and it will fall down automatically due to gravity.

示例:

rigidbody2D.AddForce(new Vector2(0, 10), ForceMode2D.Impulse);

这篇关于unity 2d 跳跃脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-24 06:41