簡體   English   中英

用Unity和C#跳一個角度

[英]Jumping at an angle with Unity and C#

我正在做一個項目,試圖通過跳躍一個角度來使角色移動。 現在,在框架更新過程中,角色將來回旋轉,按下右鍵時,角色將跳躍。 此代碼使它們跳躍一定角度,但它們始終返回到其原始位置。

另外,我有兩個角色從舞台的相反兩側開始,但是當我開始游戲時,它們會傳送到相同的位置。 我花了很多時間檢查我的代碼,但似乎無法正常工作。 您可以提供任何幫助嗎?

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語句中更新當前位置,而不是在每次更新后都更新當前位置,實際位置是根據決策而不是循環的結尾移動的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM