【问题标题】:Unity grounded state is flickering for character. .isGrounded being used but still flickering统一的接地状态正在闪烁。 .isGrounded 正在使用但仍在闪烁
【发布时间】:2022-01-31 06:48:21
【问题描述】:

我的角色控制器的接地状态似乎在每一帧都不断闪烁。据我所知,它应该检查玩家是否通过 player.isGrounded 接地,但其他东西正在将其移回。

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

public class PlayerCharacterController: MonoBehaviour {

    static Animator anim;

    public bool walking;

    public GameObject playerModel, Hero;
    //Transforms
    public Transform playerCam, character, centerPoint;

    private Vector3 moveDirection;

    //character controller declaration
    CharacterController player;

    //Mouse Rotation
    private float rotX, rotY;
    //Mouse Y Position
    public float mouseYPosition = 1f; 
    //Mouse Sensitivity
    public float Sensitivity = 10f;
    //Mouse Zoom
    private float zoom;
    public float zoomSpeed = 2;
    //Clamping Zoom
    public float zoomMin = -2f;
    public float zoomMax = -10f;
    public float rotationSpeed = 5f;
    //Move Front Back left & Right
    private float moveFB, moveLR;   
    //Movement Speed
    public float Speed = 2f;
    //Velocity of Gravity
    public float verticalVelocity;
    //Jump Distance
    public float jumpDist = 5f;
    //Multiple Jumps
    int jumpTimes;
    //To use with Dialogue Manager
    public DialogueManager DiagM;

    public AudioClip jumpSound;
    public AudioClip HurtSound;
    public AudioClip PunchSound;

    AudioSource audioSource;

    //knockback

    public float knockBackForce;
    public float knockBackTime;
    private float knockBackCounter;


    // Use this for initialization
    void Start ()   
    {
        //character controller
        player = GameObject.Find("Player").GetComponent<CharacterController> ();

        StartCoroutine(MyCoroutine(character));

        anim = GetComponent<Animator>();

        //mouse zoom
        zoom = -3;  
        centerPoint.transform.position = playerCam.transform.position;
        centerPoint.transform.parent = null;
        audioSource = GetComponent<AudioSource>();
    }

    IEnumerator MyCoroutine (Transform character)
    {
        if (player.isGrounded == true)

        {
        anim.SetBool("isFalling",false);
        //anim.SetBool("isIdling", true);
        yield return new WaitForSeconds(0);
        }

    }
    // Update is called once per frame

    void Update ()
    {        
        //Mouse Zoom Input
        zoom += Input.GetAxis ("Mouse ScrollWheel") * zoomSpeed;

        if (zoom > zoomMin)
            zoom = zoomMin;
        if (zoom < zoomMax)
            zoom = zoomMax;

        //Mouse Camera Input
        playerCam.transform.localPosition = new Vector3 (0, 0, zoom);

        //Mouse Rotation

        rotX += Input.GetAxis ("Mouse X") * Sensitivity;
        rotY -= Input.GetAxis ("Mouse Y") * Sensitivity;      

        //Clamp Camera
        rotY = Mathf.Clamp (rotY, -60f, 60f);
        playerCam.LookAt (centerPoint);
        centerPoint.localRotation = Quaternion.Euler (rotY, rotX, 0);

        //Movement Speed
        if (knockBackCounter <= 0)
        {   
            moveDirection = (transform.forward * Input.GetAxis("Vertical")) + (transform.right * Input.GetAxis("Horizontal"));
            moveDirection = moveDirection * Speed;
            moveDirection.y = verticalVelocity;

            player.Move(moveDirection * Time.deltaTime);



            //Movement Rotation

            centerPoint.position = new Vector3 (character.position.x, character.position.y + mouseYPosition, character.position.z);

            //knockback disable
            //Movement Input

            if (Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0)

            {
                transform.rotation = Quaternion.Euler(0f, centerPoint.rotation.eulerAngles.y, 0f);
                Quaternion turnAngle = Quaternion.LookRotation(new Vector3(moveDirection.x, 0f, moveDirection.z));
                playerModel.transform.rotation = Quaternion.Slerp(playerModel.transform.rotation, turnAngle, Time.deltaTime * rotationSpeed);

                if (player.isGrounded == true)
                {
                    anim.SetBool("isWalking", true);
                    anim.Play("Running");
                }
            }
            else 
            {
                StartCoroutine(MyCoroutine(character));
            }

            if (Input.GetButtonDown("LHand"))
            {
                audioSource.PlayOneShot(PunchSound, 1F);
                anim.Play("RPunch");
            }

            if (player.isGrounded == true)
            {
                jumpTimes = 0;
                //verticalVelocity = -Physics.gravity.y * Time.deltaTime;
                verticalVelocity = 0;
            }
            else
            {
                verticalVelocity += Physics.gravity.y * Time.deltaTime;
                anim.SetBool("isWalking", false);
                anim.SetBool("isFalling", true);
            }


            if (jumpTimes < 1)
            {
                if (Input.GetButtonDown("Jump"))
                {
                    verticalVelocity += jumpDist;
                    anim.Play("Jump");
                    audioSource.PlayOneShot(jumpSound, 1F);
                    jumpTimes += 1;
                }
            }
        }
        else
        {
            knockBackCounter -= Time.deltaTime;
        }
    }

    public void Knockback(Vector3 direction)
    {
        knockBackCounter = knockBackTime;
        anim.Play("Jump");
        audioSource.PlayOneShot(HurtSound, 50F);
        moveDirection = direction * knockBackForce;
        moveDirection.y = knockBackForce;
    }
}

看起来它与verticalVelocity 线有关,但到目前为止我只尝试设置verticalVelocity = 0 并且在我实际移动角色之前一直有效。我可以改变什么来停止闪烁?

【问题讨论】:

  • 添加您用来检查播放器是否接地的代码。可能有问题。
  • 我可以通过检查动画参数“isFalling”来确定它是否接地。它确实快速闪烁。

标签: c# unity3d


【解决方案1】:

可能已经解决了,但原因是如果您使用角色控制器,您应该始终对角色应用重力。

当角色与一个物体碰撞时,它实际上是进入了这个物体内部一点点,然后 Unity 将角色推回远离该物体,直到它不再接触它。此时,您的重力再次开始作用,并重新启动循环。

您需要在 100% 的时间内施加重力以产生足够的力来“平衡”这场与地板的战斗。可以是较小的“重力”,例如 1。不需要成为您的重力变量。

另外,除此之外,我还喜欢添加一个“Coyote time”,并让我的 on IsGrounded() 方法如下:

    public bool IsGrounded()
{
    return CoyoteTime < CoyoteTimeMax;
}

public void CoyoteControl()
{
    if (CharController.isGrounded)
    {
        CoyoteTime = 0;
    }
    else
    {
        CoyoteTime += Time.deltaTime;
    }
}

然后我在 Update() 上调用 CoyoteControl(),我可以在需要时调用 IsGrounded()。 在检查器上,我通常将 CoyoteTimeMax 设置为 0.1,这样可以使下落更加平滑。

【讨论】:

    【解决方案2】:

    根据您的评论。您不应该通过检查动画参数来确定您的播放器是否接地。最好的方法是使用 RayCast()。那么你必须做什么:

    1. 创建一个名为Ground的层,并将所有平台添加到你的 场景到该图层。
    2. 创建一个布尔变量

    即

    bool isGrounded;
    
    1. 创建一个函数来检查角色是否接地

    类似:

    bool checkGrounded(){
    
        return Physics.Raycast(transform.position, Vector3.down, 2f, 1 << LayerMask.NameToLayer("Ground")));
    
    } 
    

    在answer 中,您可以了解 Raycast 中涉及的参数

    1. 最后在更新中检查玩家是否接地

    类似:

    void Update(){
        isGrounded = checkGrounded();
    }
    

    【讨论】:

      【解决方案3】:

      我发现如果您多次检查 isGrounded 检查,它可能会在 Update() 函数的过程中发生变化。在函数开头将其分配给变量可能会解决闪烁问题。

      void Update()
      {
          bool isGrounded = characterController.isGrounded;
          ...
      

      【讨论】:

        猜你喜欢
        • 2013-01-21
        • 2019-12-06
        • 1970-01-01
        • 1970-01-01
        • 2019-04-23
        • 2012-09-15
        • 2017-05-24
        • 1970-01-01
        • 2021-12-17
        相关资源
        最近更新 更多