如何使用Unity中的按钮移动字符

时间:2019-05-26 00:03:11

标签: c# unity3d game-development

我使用Unity创建了我的第一个应用程序,但是要使其在Android上运行,我必须放置一些控制按钮,问题是按钮跳转并不想起作用,我也不知道该如何移动按钮。我正在使用Unity 4,并且想移动角色第一人称视角。

我找到了此代码,但对我而言不起作用:

using UnityEngine;
using System.Collections;

public class jump_1 : MonoBehaviour {
public float speed = 6.0f;
public float gravity = -9.8f;

private float verticalVelocity;
private float jumpForce = 15.0f;
private float gravityJump = 14.0f;

private CharacterController _charController;

void Start() {
    _charController = GetComponent<CharacterController>();
}

void Update() {
    float deltaX = Input.GetAxis("Horizontal") * speed;
    float deltaZ = Input.GetAxis("Vertical") * speed;
    Vector3 movement = new Vector3(deltaX, 0, deltaZ);
    movement = Vector3.ClampMagnitude(movement, speed);

    movement.y = gravity;

    movement *= Time.deltaTime;
    movement = transform.TransformDirection(movement);
    _charController.Move(movement);


    if (_charController.isGrounded) {
        verticalVelocity = -gravityJump * Time.deltaTime;
        if (Input.GetKeyDown (KeyCode.Space)) {
            verticalVelocity = jumpForce;
        }
    } else {
        verticalVelocity -= gravityJump * Time.deltaTime;
    }
Vector3 jumpVector = new Vector3(0, verticalVelocity, 0);
    _charController.Move (jumpVector * Time.deltaTime);
}
}

0 个答案:

没有答案