角色在无尽的亚军游戏中具有角色控制器的抖动

时间:2019-04-05 16:34:44

标签: c# visual-studio unity3d character jitter

我已经统一制作了《无尽的亚军》游戏,但问题是角色非常紧张

我尝试了多种方法来修复它,但对我没有任何帮助,我也尝试过调整字符控制器中的值,尤其是皮肤宽度,但没有任何帮助,您可以在此处的视频中看到问题 https://drive.google.com/open?id=1Zrtc2o7vLQlESpvXmz1zy6hp_6abGJzK

所使用的代码也已随附。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMotor : MonoBehaviour
{

    public AudioSource JumpAudio;
    public AudioSource SlideAudio;
    public AudioSource DieAudio;

    private const float LANE_DISTANCE = 2.5f;
    private const float TURN_SPEED=0.05f;

    //
    private bool isRunning = false;

    //Animation

    private Animator anim;

    //Movement
    private CharacterController controller;
    private float jumpForce = 10.0F;              // how high player can jump
    private float gravity = 12.0f;         //Gravity makes the body fall down
    private float verticalVelocity; //This will be set for every single frame

    private int desiredLane=1;       // 0=Left , 1= Middle , 2=Right

    //Speed Modifier
    private float originalSpeed= 8.0f;
    private float Speed = 8.0f;                     // Player Speed
    private float speedIncreaseLastTick;    // Last time speed was increased
    private float speedIncreaseTime = 2.5f;
    private float speedIncreaseAmount=0.1f;


    private void Start()
    {

        Speed = originalSpeed;
        controller = GetComponent<CharacterController>();
        anim = GetComponent<Animator>();
    }

    private void Update()
    {
        if (!isRunning)
            return;

        if(Time.time - speedIncreaseLastTick > speedIncreaseTime)
        {
            speedIncreaseLastTick = Time.time;
            Speed += speedIncreaseAmount;
            GameManager.Instance.UpdateModifier(Speed-originalSpeed);
        }

        //Gather the inputs on which lane we should be
        if (MobileInput.Instance.SwipeLeft)
            MoveLane(false);
        if (MobileInput.Instance.SwipeRight)
            MoveLane(true);

        // Calculate Where we should be in the future
        Vector3 targetPosition = transform.position.z * Vector3.forward;
        if (desiredLane == 0)
            targetPosition += Vector3.left * LANE_DISTANCE;
        else if (desiredLane == 2)
            targetPosition += Vector3.right * LANE_DISTANCE;

        // Calculate our move dalta
        Vector3 moveVector = Vector3.zero;
        moveVector.x = (targetPosition - transform.position).normalized.x * 
        Speed;  //where we should be - where we are right now  , directional
        vector

        bool isGrounded = IsGrounded();
        anim.SetBool("Grounded", isGrounded);

        // Calculate Y
        if (IsGrounded()) //if Grounded
        {
            verticalVelocity = -0.1f;

            if (MobileInput.Instance.SwipeUp)
            {
                //Jump
                //  anim.SetTrigger("Jump");
                anim.SetBool("Jump", true);
                verticalVelocity = jumpForce;
                JumpAudio.Play();
                Invoke("Stopjump", 1.0f);

            }
            else if (MobileInput.Instance.SwipeDown)
            {
                //Slide
                StartSliding();
                Invoke("StopSliding", 1.0f);
                SlideAudio.Play();
            }
        }
        else
        {
            verticalVelocity -= (gravity * Time.deltaTime);

            //Fast falling Mechanics while in the air
            if(MobileInput.Instance.SwipeDown)
            {
                verticalVelocity = -jumpForce;
            }

        }
        moveVector.y = verticalVelocity;
        moveVector.z = Speed;

        //Move the player
        controller.Move(moveVector * Time.deltaTime);

        //Rotate the player to where he is going
        Vector3 dir = controller.velocity;
        if(dir!= Vector3.zero)
        {
            dir.y = 0;
            transform.forward = Vector3.Lerp(transform.forward, dir, 
            TURN_SPEED);
        }


    }

    private void StartSliding()
    {
        anim.SetBool("Sliding", true);
        controller.height /= 2;
        controller.center = new Vector3(controller.center.x, 
        controller.center.y/2,controller.center.z);
    }

    private void StopSliding()
    {
        anim.SetBool("Sliding", false);
        controller.height *= 2;
        controller.center = new Vector3(controller.center.x, 
        controller.center.y * 2, controller.center.z);
    }
    private void Stopjump()
    {
        anim.SetBool("Jump", false);
        // controller.height *= 2;
        //   controller.center = new Vector3(controller.center.x, 
        controller.center.y * 2, controller.center.z);
    }
    public void MoveLane(bool goingRight)
    {

        desiredLane += (goingRight) ? 1 : -1;
        desiredLane = Mathf.Clamp(desiredLane, 0, 2);


    }

    private bool IsGrounded()
    {
        Ray groundray = new Ray(
            new Vector3(
                controller.bounds.center.x,
                (controller.bounds.center.y - controller.bounds.extents.y) + 
                0.02f,
                controller.bounds.center.z),
            Vector3.down);
        Debug.DrawRay(groundray.origin, groundray.direction, Color.cyan, 
        1.0f);

        return Physics.Raycast(groundray, 0.2f + 0.1f);

    }

    public void StartRunning()
    {
        isRunning = true;
        anim.SetTrigger("StartRunning");
    }

    private void Crash()
    {
        anim.SetTrigger("Death");
        isRunning = false;
        GameManager.Instance.OnDeath();
        DieAudio.Play();
    }
    private void OnControllerColliderHit(ControllerColliderHit hit)
    {
      switch (hit.gameObject.tag)
        {
            case "Obstacle":
                Crash();
                break;
        }



    }

}

0 个答案:

没有答案