如何将跳跃应用到控制器和播放器,以实现统一的 3D 无尽奔跑者
How to apply jump both to controller and player for an endless runner 3D in unity
我正在尝试在 unity 5 中开发一款 3D 无尽跑酷游戏。我面临的问题是跳跃。如何将跳跃应用于无尽的跑步者?我一直在互联网上搜索和冲浪,但没有运气。我确实找到了一些代码,但是当我尝试应用它们时,它们不起作用。另外,如何调整角色控制器以配合动画?非常感谢您的帮助。
这是 playerMotor.cs 代码。
private Vector3 moveVector;
private float verticalVelocity = 0.0f;
private float gravity = 12.0f;
private float jumpPower = 15.0f;
private bool jump;
private float animationDuration = 3.0f;
private float startTime;
private Animator anim;
private CharacterController controller;
void Start()
{
controller = GetComponent<CharacterController>();
anim = GetComponent<Animator>();
startTime = Time.time;
}
void Update()
{
if (Time.time - startTime < animationDuration)
{
controller.Move(Vector3.forward * speed * Time.deltaTime);
return;
}
moveVector = Vector3.zero;
if (controller.isGrounded)
{
verticalVelocity = -0.5f;
if (Input.GetButton("Jump"))
{
verticalVelocity = jumpPower;
anim.SetBool("Jump", true);
}
//anim.SetBool("Jump", false);
}
else
{
verticalVelocity -= gravity * Time.deltaTime;
}
// X - Left and Right
moveVector.x = Input.GetAxisRaw("Horizontal") * speed;
anim.SetFloat("Turn", moveVector.x);
if (Input.GetMouseButton(0))
{
//Are we holding touch on the right side?
if (Input.mousePosition.x > Screen.width / 2)
{
moveVector.x = speed;
}
else
{
moveVector.x = -speed;
}
}
//// Y - Up and Down
//if (Input.GetButtonDown("Jump"))
//{
// moveVector.y = verticalVelocity;
//}
// Z - Forward and Backward
moveVector.z = speed;
controller.Move(moveVector * Time.deltaTime);
}
我实现的代码如下所示。
function PlayerController(){
var controller : CharacterController = GetComponent(CharacterController);
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (controller.isGrounded) {
vSpeed = -1;
if (Input.GetButtonDown ("Jump")) {
vSpeed = jumpSpeed;
}
}
vSpeed -= gravity * Time.deltaTime;
moveDirection.y = vSpeed;
controller.Move(moveDirection * Time.deltaTime);
}
我已经为普通RPG角色应用了跳跃,但是这个比我想象的要难。
从你的评论来看,你的实际问题似乎是 "controller.isGrounded" 从来没有出现过,对吗?
我在与动画相关的评论 (anim.SetBool("Jump", false);
) 中描述了第一个问题。
第二个问题来自您的 PlayerController
代码。
您 "reset" 您的跳跃设置 vSpeed = -1;
,就在您刚刚跳跃的那一帧之后。所以从技术上讲,引擎甚至没有时间对跳跃做出反应,因为下一帧的角色会被硬拉回地面(通过 "anti-jump" :) 你实现了)。
我的建议是,从 CharacterController.Move 中获取示例代码,就像您在应用更改之前所做的那样,但这次不要更改它!
只需将代码段复制粘贴到您的应用程序中并进行测试。在你让它工作后 "as is",再次,在代码中没有任何更改,添加你想要的自定义,一个一个 并且每次测试是否更改引入了缺陷( bug).
我还建议你在编码时开始使用 Debug.Log()
这样你就可以过滤掉像现在这样的问题,因为你'''ll see what happens in the code "on the fly", when you play-test (.Log variable values with comments, if "branches" when they tick in, calculated values, calls to important functions-like .Move( )- 等).
我希望这有帮助!干杯!
我终于找到了问题的答案。谢谢马克给我的建议,对我帮助很大。
这就是我为实现跳跃和跳跃动画所做的工作。另请注意,我发布的是正确的而不是整个代码,因为我在脚本的其余部分没有发现任何其他错误。
moveVector = Vector3.zero;
if (controller.isGrounded) //Check whether are we ground
{
verticalVelocity = -0.5f; //Just to make sure that we are ground
if (Input.GetButton("Jump"))
{
verticalVelocity = 10f; //Basically this is the Jump power to Player
anim.SetBool("Jump", true); //Jump animation to true
}
}
else
{
verticalVelocity -= gravity * speed * Time.deltaTime; //Apply gravity
anim.SetBool("Jump", false); //Set jump animation to false to stop looping continuously
}
//... the rest
这是我自己的代码。感谢您帮助马克。也为你干杯。
我正在尝试在 unity 5 中开发一款 3D 无尽跑酷游戏。我面临的问题是跳跃。如何将跳跃应用于无尽的跑步者?我一直在互联网上搜索和冲浪,但没有运气。我确实找到了一些代码,但是当我尝试应用它们时,它们不起作用。另外,如何调整角色控制器以配合动画?非常感谢您的帮助。
这是 playerMotor.cs 代码。
private Vector3 moveVector;
private float verticalVelocity = 0.0f;
private float gravity = 12.0f;
private float jumpPower = 15.0f;
private bool jump;
private float animationDuration = 3.0f;
private float startTime;
private Animator anim;
private CharacterController controller;
void Start()
{
controller = GetComponent<CharacterController>();
anim = GetComponent<Animator>();
startTime = Time.time;
}
void Update()
{
if (Time.time - startTime < animationDuration)
{
controller.Move(Vector3.forward * speed * Time.deltaTime);
return;
}
moveVector = Vector3.zero;
if (controller.isGrounded)
{
verticalVelocity = -0.5f;
if (Input.GetButton("Jump"))
{
verticalVelocity = jumpPower;
anim.SetBool("Jump", true);
}
//anim.SetBool("Jump", false);
}
else
{
verticalVelocity -= gravity * Time.deltaTime;
}
// X - Left and Right
moveVector.x = Input.GetAxisRaw("Horizontal") * speed;
anim.SetFloat("Turn", moveVector.x);
if (Input.GetMouseButton(0))
{
//Are we holding touch on the right side?
if (Input.mousePosition.x > Screen.width / 2)
{
moveVector.x = speed;
}
else
{
moveVector.x = -speed;
}
}
//// Y - Up and Down
//if (Input.GetButtonDown("Jump"))
//{
// moveVector.y = verticalVelocity;
//}
// Z - Forward and Backward
moveVector.z = speed;
controller.Move(moveVector * Time.deltaTime);
}
我实现的代码如下所示。
function PlayerController(){
var controller : CharacterController = GetComponent(CharacterController);
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (controller.isGrounded) {
vSpeed = -1;
if (Input.GetButtonDown ("Jump")) {
vSpeed = jumpSpeed;
}
}
vSpeed -= gravity * Time.deltaTime;
moveDirection.y = vSpeed;
controller.Move(moveDirection * Time.deltaTime);
}
我已经为普通RPG角色应用了跳跃,但是这个比我想象的要难。
从你的评论来看,你的实际问题似乎是 "controller.isGrounded" 从来没有出现过,对吗?
我在与动画相关的评论 (anim.SetBool("Jump", false);
) 中描述了第一个问题。
第二个问题来自您的 PlayerController
代码。
您 "reset" 您的跳跃设置 vSpeed = -1;
,就在您刚刚跳跃的那一帧之后。所以从技术上讲,引擎甚至没有时间对跳跃做出反应,因为下一帧的角色会被硬拉回地面(通过 "anti-jump" :) 你实现了)。
我的建议是,从 CharacterController.Move 中获取示例代码,就像您在应用更改之前所做的那样,但这次不要更改它!
只需将代码段复制粘贴到您的应用程序中并进行测试。在你让它工作后 "as is",再次,在代码中没有任何更改,添加你想要的自定义,一个一个 并且每次测试是否更改引入了缺陷( bug).
我还建议你在编码时开始使用 Debug.Log()
这样你就可以过滤掉像现在这样的问题,因为你'''ll see what happens in the code "on the fly", when you play-test (.Log variable values with comments, if "branches" when they tick in, calculated values, calls to important functions-like .Move( )- 等).
我希望这有帮助!干杯!
我终于找到了问题的答案。谢谢马克给我的建议,对我帮助很大。
这就是我为实现跳跃和跳跃动画所做的工作。另请注意,我发布的是正确的而不是整个代码,因为我在脚本的其余部分没有发现任何其他错误。
moveVector = Vector3.zero;
if (controller.isGrounded) //Check whether are we ground
{
verticalVelocity = -0.5f; //Just to make sure that we are ground
if (Input.GetButton("Jump"))
{
verticalVelocity = 10f; //Basically this is the Jump power to Player
anim.SetBool("Jump", true); //Jump animation to true
}
}
else
{
verticalVelocity -= gravity * speed * Time.deltaTime; //Apply gravity
anim.SetBool("Jump", false); //Set jump animation to false to stop looping continuously
}
//... the rest
这是我自己的代码。感谢您帮助马克。也为你干杯。