gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:38:21 24 4
gpt4 key购买 nike

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

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com