如何在控制器和播放器上同时应用跳跃,以实现无尽奔跑的3D统一 [英] How to apply jump both to controller and player for an endless runner 3D in unity
问题描述
我正在尝试以单位5开发3D无尽的跑步游戏.我面临的问题是跳跃.如何对无尽的跑步者施加跳跃?我一直在通过互联网搜索和冲浪,但是没有运气.我确实找到了一些代码,但是当我尝试应用它们时,它们没有用.另外,如何调整角色控制器以适应动画?非常感谢您的帮助.
I'm trying to develop a 3D endless runner game in unity 5. The problem I'm facing is jump. How can I apply a jump to an endless runner? I've been searching and surfing through the internet with no luck. I did find some codes but when I try to apply them, they didn't work. Also How do I adjust the character controller to go up with the animation? A help would be really appreciated.
这是playerMotor.cs代码.
This is the playerMotor.cs code.
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);
}
我用来实现的代码如下所示.
The codes I used to implement is shown below.
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角色应用了跳转功能,但这一步比我想象的要难.
I have applied jump for normal RPG characters but this one is harder than I thought.
推荐答案
我终于找到了我的问题的答案.感谢Mark为您提供的建议,它对我有很大帮助.
I finally found the answer to my question. Thanks Mark for the advice you gave me, it helped me a lot.
所以这是我做跳跃和跳跃动画发生的工作.另请注意,由于我在脚本的其余部分中未发现任何其他错误,因此我发布的不是整个代码正确的内容.
So here's what I did to make the jump and the jump animation happen. Also note that I'm posting what which is correct not the whole code since I didn't find any other errors in the rest of the script.
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
这是我自己的代码.感谢您对Mark的帮助.也为您加油.
This is my own code. Thanks for helping Mark. Cheers to you too.
这篇关于如何在控制器和播放器上同时应用跳跃,以实现无尽奔跑的3D统一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!