c# - 使用 Unity 和 C# 以一定角度跳跃

标签 c# unity3d

我正在做一个项目,我试图通过以一定角度跳跃来让我的角色移动。现在在帧更新期间,角色会来回旋转,当按下右键时,他们会跳跃。此代码使它们以一定角度跳跃,但它们始终会返回到原来的位置。

此外,我有两个角色,他们从舞台的两侧开始,但当我开始游戏时,他们传送到相同的位置。我花了很多时间检查我的代码,但我似乎无法让它工作。您能提供什么帮助?

using UnityEngine;
using System.Collections;

public class Freg : MonoBehaviour {
    public GameObject Tounge;
    public float gravity;
    public float tempScale = 1;
    public float MaxJump = 8f;
    public float MinJump = 0.1f;
    static float yVector = 0;
    static float xVector = 0;
    static bool grounded = true;
    bool isleft = false;
    Vector3 farthestleft;
    Vector3 farthestright;

    // Use this for initialization
    void Start () {
        farthestleft = new Vector3 (-33.7f, 50.2f, 24.8f);
        farthestright = new Vector3 (22.56f, 54.83f, -15.12f);
    }

    void OnTriggerEnter (Collider other) {
        if (other.GetComponent<Collider> ().tag == "Ground") {
            grounded = true;
            yVector = 0;
            //xVector = 0;
            Vector3 onGround = new Vector3 (transform.position.x, -4.86f, transform.position.z);
            transform.position = onGround;
        } else
            grounded = false;
    }

    // Update is called once per frame
    void Update () {
        /*if (Input.GetKey (KeyCode.UpArrow) == true) {
            Tounge.transform.localScale.Set (1, 0.5f, 1);
        } else {
            Tounge.transform.localScale.Set (1, 1, 1);
        }*/

        if (grounded == false) {
            yVector -= gravity;
        }
        if (Input.GetKeyDown (KeyCode.UpArrow) == true && grounded == true) {
            MinJump += 0.5f;
        } else if (MinJump > 0.1f){
            yVector += MinJump;
            xVector += MinJump;
            MinJump = 0.1f;
            grounded = false;
        }
        Vector3 stuff = new Vector3 (transform.localPosition.y + xVector, transform.position.y + yVector, transform.position.z);
        transform.position = stuff;
        float t = Mathf.PingPong (Time.time * 0.5f * 2.0f, 1.0f);
        transform.eulerAngles = Vector3.Lerp (farthestright, farthestleft, t);

}
}

最佳答案

看起来您应该在 if 语句期间更新当前位置,而不是在每次更新之后以这种方式更新,实际位置是根据决策而不仅仅是循环结束而移动的。

关于c# - 使用 Unity 和 C# 以一定角度跳跃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31058466/

相关文章:

c# - list<> 是否存储对属性或实例的引用?

c# - 获取 Azure 虚拟机状态

ios - 在 Unity iOS 上打开设置应用程序

c# - Unity3D-Google Play 64位要求存在问题

unity3d - Unity 项目是否有标准/推荐的目录结构?

c# - 反序列化多维 JSON 字符串

c# - Facebook 的 OAuthWebSecurity 未按预期使用电子邮件权限

c# - 备份和恢复mysql数据库c#

ios - 如何在 Unity for iOS 中禁用 Firebase?

android - Vuforia SDK Android Studio VS Vuforia Unity