Jumping at an angle with Unity and C#

时间:2015-06-25 18:50:17

标签: c# unity3d

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); } }

1 个答案:

答案 0 :(得分:0)

看起来你应该在if语句中更新当前位置,而不是在每次更新之后更新当前位置,实际位置根据决策而不仅仅是循环结束而移动。