如何在Unity3D中使GameObject跳跃

本文关键字:GameObject 跳跃 中使 Unity3D | 更新日期: 2023-09-27 18:15:44

我有一款2d游戏,其中GameObject (GO)移动到地板的右侧。我想让它跳起来。

但是当我将字符控制器添加到GO并开始模拟时,它只是在左侧快速移动。我不明白,怎么了。

GO还包含RigidbodyBox Collider

    public float jumpSpeed = 8.0f;
    public float gravity = 20.0f;
    private Vector3 moveDirection = Vector3.zero;
    void Start ()
    {
        player = (GameObject)this.gameObject;
    }
    void Update ()
    {
        // Move
        player.transform.position +=
            player.transform.right * speed * Time.deltaTime;
        // Jump    
        CharacterController controller = GetComponent<CharacterController>();
        if (controller.isGrounded)
        {
            moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0,
                                        Input.GetAxis("Vertical"));
            moveDirection = transform.TransformDirection(moveDirection);
            moveDirection *= speed;
            if(Input.GetKey(KeyCode.Space))
                moveDirection.y = jumpSpeed;
        }
        moveDirection.y -= gravity * Time.deltaTime;
        controller.Move (moveDirection * Time.deltaTime);
    }

如何在Unity3D中使GameObject跳跃

这是旧的,但在谷歌搜索中偶然发现的

Character Controller不是设计用来使用物理组件的。移除刚体和盒子碰撞器。它使用模拟胶囊对撞机进行检测。

也从你的代码中删除以下内容,输入应该再次正常工作。

player.transform.position += player.transform.right * speed * time.deltaTime;    

这段代码导致您在无意中移动。

你应该首先用下面的代码创建PlayerController类:

public class PlayerController : MonoBehaviour {
    public Vector2 moving = new Vector2();
    // Use this for initialization
    void Start () {
    }
    // Update is called once per frame
    void Update () {
        moving.x = moving.y = 0;
        if (Input.GetKey ("right")) {
            moving.x = 1;
        } else if (Input.GetKey ("left")) {
            moving.x = -1;
        }
        if (Input.GetKey ("up")) {
            moving.y = 1;
        } else if (Input.GetKey ("down")) {
            moving.y = -1;
        }
    }
}
然后用下面的代码创建Player类:
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
    public float speed = 10f;
    public Vector2 maxVelocity = new Vector2(3, 5);
    public bool standing;
    public float jetSpeed = 15f;
    public float airSpeedMultiplier = .3f;
    public float jump=2f ;

    private PlayerController controller;
    void Start(){
        controller = GetComponent<PlayerController> ();
    }
    // Update is called once per frame
    void Update () {
        var forceX = 0f;
        var forceY = 0f;
        var absVelX = Mathf.Abs (rigidbody2D.velocity.x);
        var absVelY = Mathf.Abs (rigidbody2D.velocity.y);
        if (absVelY < .2f) {
            standing = true;
        } else {
            standing = false;
        }
        if (controller.moving.x != 0) {
            if(absVelX < maxVelocity.x){
                forceX = standing ? speed * controller.moving.x : (speed * controller.moving.x * airSpeedMultiplier);
                transform.localScale = new Vector3(forceX > 0 ? 1 : -1, 1, 1);
            }
        }
        if (controller.moving.y > 0) {
            if(absVelY < maxVelocity.y)
                forceY = jetSpeed * controller.moving.y;
        }
        rigidbody2D.AddForce (new Vector2 (forceX, forceY));
    if (Input.GetKey ("a")) {
            if (transform.localPosition.y < 1   ) 
                rigidbody2D.AddForce (new Vector2 (0, jump ));

                }
    }
}

你可以用箭头键飞行,用"a"键跳跃。