C# Unity 角色跳跃真的很奇怪
C# Unity Character jumps really weird
大家好!
我的 UNITY 5 代码有问题。
我的角色可以跳,但他跳得很快而且太快了。
看起来真的很奇怪。
非常感谢您对我的代码的反馈!
using UnityEngine;
using System.Collections;
public class Gravity : MonoBehaviour {
private float inputDirection;
private float VerticalSpeed;
private float gravity = -2f;
private float speedmultiplier = 5f;
private bool jump;
private Vector3 moveVector;
private CharacterController controller;
// Use this for initialization
void Start () {
controller = GetComponent<CharacterController> ();
}
// Update is called once per frame
void Update () {
inputDirection = Input.GetAxis ("Horizontal");
moveVector = new Vector3 (inputDirection, VerticalSpeed, 0) * speedmultiplier;
controller.Move (moveVector * Time.deltaTime);
if (controller.isGrounded) {
VerticalSpeed = 0.0f;
jump = true;
} else {
VerticalSpeed = gravity;
jump = false;
}
if (Input.GetKey(KeyCode.X)) {
if(jump == true) {
VerticalSpeed += 25f;
}
}
}
}
您可以尝试更改 else
中的垂直速度以反映随时间的变化。
可能是这样的:
VerticalSpeed += gravity * Time.deltaTime
而不是仅仅将其设置为重力。你可能需要调整你的初始跳跃速度来让它感觉更好,但这应该让你开始快速跳跃,当你到达跳跃的顶部时减慢速度,并在你下降时加速回来。
大家好!
我的 UNITY 5 代码有问题。
我的角色可以跳,但他跳得很快而且太快了。 看起来真的很奇怪。
非常感谢您对我的代码的反馈!
using UnityEngine;
using System.Collections;
public class Gravity : MonoBehaviour {
private float inputDirection;
private float VerticalSpeed;
private float gravity = -2f;
private float speedmultiplier = 5f;
private bool jump;
private Vector3 moveVector;
private CharacterController controller;
// Use this for initialization
void Start () {
controller = GetComponent<CharacterController> ();
}
// Update is called once per frame
void Update () {
inputDirection = Input.GetAxis ("Horizontal");
moveVector = new Vector3 (inputDirection, VerticalSpeed, 0) * speedmultiplier;
controller.Move (moveVector * Time.deltaTime);
if (controller.isGrounded) {
VerticalSpeed = 0.0f;
jump = true;
} else {
VerticalSpeed = gravity;
jump = false;
}
if (Input.GetKey(KeyCode.X)) {
if(jump == true) {
VerticalSpeed += 25f;
}
}
}
}
您可以尝试更改 else
中的垂直速度以反映随时间的变化。
可能是这样的:
VerticalSpeed += gravity * Time.deltaTime
而不是仅仅将其设置为重力。你可能需要调整你的初始跳跃速度来让它感觉更好,但这应该让你开始快速跳跃,当你到达跳跃的顶部时减慢速度,并在你下降时加速回来。