简体   繁体   English

用Unity和C#跳一个角度

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

I'm working on a project where I'm trying to make my character move by jumping at an angle. 我正在做一个项目,试图通过跳跃一个角度来使角色移动。 Right now during the frame updates, the character will pivot back and forth and when the right key is pressed, they will jump. 现在,在框架更新过程中,角色将来回旋转,按下右键时,角色将跳跃。 This code causes them to jump at an angle, but they always return to their original position. 此代码使它们跳跃一定角度,但它们始终返回到其原始位置。

Additionally, I have two characters who start on opposite sides of the stage, but when I start the game they teleport to the same position. 另外,我有两个角色从舞台的相反两侧开始,但是当我开始游戏时,它们会传送到相同的位置。 I've spent a lot of time reviewing my code but I can't seem to get this to work. 我花了很多时间检查我的代码,但似乎无法正常工作。 Any help you can provide? 您可以提供任何帮助吗?

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