简体   繁体   English

如何在控制器和播放器上同时应用跳跃,以实现统一的无尽奔跑者3D

[英]How to apply jump both to controller and player for an endless runner 3D in unity

I'm trying to develop a 3D endless runner game in unity 5. The problem I'm facing is jump. 我正在尝试以单位5开发3D无尽的跑步游戏。我面临的问题是跳跃。 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. 一个帮助将不胜感激。

This is the playerMotor.cs code. 这是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);
}

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

I have applied jump for normal RPG characters but this one is harder than I thought. 我已经为普通的RPG角色应用了跳转功能,但是这一步比我想象的要难。

From your comment it seems your actual problem is "controller.isGrounded" never ticks in, right? 从您的评论看来,您的实际问题似乎是“ controller.isGrounded”永远不会出现,对吗?
I described the first issue in comment ( anim.SetBool("Jump", false); ) related to animation. 我在注释中描述了与动画有关的第一个问题( anim.SetBool("Jump", false); )。
This second problem comes from your PlayerController code. 第二个问题来自您的PlayerController代码。

You "reset" your jump with setting vSpeed = -1; 您可以通过设置vSpeed = -1; “重置”跳跃vSpeed = -1; , right after the frame you just jumped. ,刚跳完画面之后。 So techincally, the engine doesn't even have time to react on the jump as the character the next frame gets pulled back hard to the ground (by the "anti-jump" :) you implemented). 因此,从技术上讲,引擎甚至没有时间对跳跃做出反应,因为下一帧的角色被硬拉回地面(通过实现的“ anti-jump” :))。

What I recommend is, take the sample code from CharacterController.Move , just like you did before you applied your changes, but this time don't alter it! 我建议您像从应用更改之前所做的那样,从CharacterController.Move中获取示例代码,但是这次不要更改它!
Just copy-paste the snippet into your app and test. 只需将代码段复制粘贴到您的应用中并进行测试。 After you made it work "as is", again, without any changes in the code, add the customizations you want, one by one and test every time if the change introduced a defect (bug). 在使它“按原样”工作之后,再次在代码中不做任何更改, 一次一个地添加所需的自定义项,并每次更改更改引入缺陷(错误)时进行测试。


I'd also recommend you to start using Debug.Log() while you are coding so you can filter out issues like what you have now, as you'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()- etc). 我还建议您在编码时开始使用Debug.Log() ,这样您就可以过滤掉像现在这样的问题,因为当您播放以下内容时,您会看到代码“动态”地发生了什么:测试(用注释记录变量值,如果在输入时显示“分支”,则计算值将调用重要函数,如.Move()等)。


I hope this helps! 我希望这有帮助! Cheers! 干杯!

I finally found the answer to my question. 我终于找到了我问题的答案。 Thanks Mark for the advice you gave me, it helped me a lot. 感谢Mark为您提供的建议,它对我有很大帮助。

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

This is my own code. 这是我自己的代码。 Thanks for helping Mark. 感谢您对Mark的帮助。 Cheers to you too. 也为您加油。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM