Unity 2d跳跃脚本 [英] Unity 2d jumping script

查看:504
本文介绍了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跳跃脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆