如何在Unity3D中使GameObject跳转

时间:2014-07-31 12:26:17

标签: c# unity3d

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

但是当我将角色控制器添加到我的GO并开始模拟时,它只是在左侧快速移动。无法理解,有什么不对。

GO还包含 Rigidbody Box 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);
    }

2 个答案:

答案 0 :(得分:2)

这是旧的,但在谷歌搜索中遇到了这个。

字符控制器未设计为使用物理组件。拆下刚体和箱式对撞机。它使用模拟的胶囊对撞机进行检测。

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

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

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

答案 1 :(得分:1)

您应首先使用以下代码创建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”键跳转。你可以简单地改变它。