首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用CineMachine的第三人RigidBody移动脚本

使用CineMachine的第三人RigidBody移动脚本
EN

Stack Overflow用户
提问于 2021-03-23 23:05:39
回答 1查看 1.1K关注 0票数 0

我正在尝试创建一个第三人称运动脚本使用影院作为相机,我遵循了Brackey“统一中的第三人称运动”YouTube教程。然后我改变了它的基础,从字符控制器到刚体,运动工作得很好。然而,当我移动播放器时,我的代码将刚体y轴的速度设置为0,这会对抗重力,使播放器在我移动时缓慢地抖动到地面。然而,当玩家停止移动时,角色确实会倒在地上。我所需要的就是让脚本忽略y轴,简单地倾听unity的引力。

代码语言:javascript
复制
    void Update()
{
    if (!photonView.isMine)
    {
        Destroy(GetComponentInChildren<Camera>().gameObject);
        Destroy(GetComponentInChildren<Cinemachine.CinemachineFreeLook>().gameObject);
        return;
    }

    float horizontal = Input.GetAxisRaw("Horizontal");
    float vertical = Input.GetAxisRaw("Vertical");

    isGrounded = Physics.CheckSphere(new Vector3(transform.position.x, transform.position.y - 1, transform.position.z), 0.01f, layerMask);

    Vector3 inputVector = new Vector3(horizontal, 0f, vertical).normalized;

    if (inputVector.magnitude >= 0.1f)
    {
        float targetAngle = Mathf.Atan2(inputVector.x, inputVector.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
        float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
        transform.rotation = Quaternion.Euler(0f, angle, 0f);

        Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;

        rb.velocity = moveDir.normalized * speed;
    }

    if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
    {
        rb.AddForce(Vector3.up * jumpForce);
    }
}
EN

回答 1

Stack Overflow用户

发布于 2021-03-24 06:51:33

播放器会抖动,因为在您的移动部分中,您将y速度设置为0,因为Vector3.forward返回new Vector3(0, 0, 1),而您仅围绕y轴旋转向量。可以考虑这样做,而不是这样:

代码语言:javascript
复制
Vector3 moveDir = new Vector3(transform.forward.x, rb.velocity.y, transform.forward.z);

这将保持速度不变,消除抖动。

注意:transform.forward会自动获取播放器的前进向量。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66765845

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档