Unity 简单字符控制器脚本无法按预期工作

问题描述 投票:0回答:2

我正在为这个问题挠头,真的想不通。 我试图从这个例子中实现玩家移动: https://www.youtube.com/watch?v=LNidsMesxSE 4:25 分开始,5:20 分结束

这个脚本试图将所有这些转化为统一。所以我从头开始,只想制作一个简单的运动脚本。 您可以将其插入任何 Unity 版本,将脚本放到带有 CharacterController 组件的对象上,添加一个带有倾斜网格的子对象,主对象将围绕其 Y 轴旋转并移动。

我确实建议使用一个简单的 T 形角色或至少一个长胶囊,这样您就可以比使用立方体来测试它时更好地看到发生了什么。

我遇到的奇怪问题是对象随机痉挛,即使我总是只在每一帧添加极小的旋转和移动。 95% 的时间它不会发生,所以我无法准确指出是什么原因造成的。

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

public class CharacterMove : MonoBehaviour
{
public float speed = 200f;
public float rotationSpeed = 180f;

public float tiltFactor = 8f;

public bool normalizeVelocity = false;
CharacterController cc;
Vector3 velocity;

Transform armatureBody;


void Start()
{
    cc = GetComponent<CharacterController>();
    armatureBody = transform.GetChild(0);
}

void Update()
{
    velocity = new Vector3(Input.GetAxis("Horizontal"), 0,
                           Input.GetAxis("Vertical"));
}

void FixedUpdate()
{
    if ((velocity != Vector3.zero))
    {
        var ccVelocity = cc.velocity; // CharacterController velocity. This might be the problem, but without it I cannot rotate the object towards the actual forward velocity.


        Quaternion toRotation;

        toRotation = Quaternion.LookRotation(ccVelocity, Vector3.up);
        transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, rotationSpeed * Time.deltaTime);

        if (Input.GetKey(KeyCode.S)) print("new toRotation: " + toRotation);
        armatureBody.localEulerAngles = new Vector3(velocity.z * tiltFactor, 0, -velocity.x * tiltFactor); // Tilt Body towards velocity This causes the weirdest and most consistest twitching bugs when the tilt is backwards so the object is moving backwards, maybe because im using LookRotation to look backwards?

    }

    velocity = Quaternion.Euler(0, transform.eulerAngles.y, 0) * velocity; // forward is always forward

    cc.SimpleMove(velocity * Time.deltaTime * speed);
}

}

这个脚本做了 4 件事,

  1. 从玩家输入中获取速度。
  2. 将主要对象(此脚本)旋转到速度
  3. 向子 armatureBody 添加朝向速度的倾斜,在我的例子中这是一个 t Pose 角色。
  4. 移动此脚本所在的主要对象。

任何帮助将不胜感激。

我尝试删除 vector3 != 零检查,我尝试对向量进行归一化,我尝试使用不同的四元数旋转方法,但都具有相同的错误结果。

c# unity-game-engine rotation position game-development
2个回答
0
投票

如果 Input.forward > threshold 如果旋转到速度,则检查 Update()。不要只在侧身或后退时旋转到速度,所以我们现在可以在站立时侧身扫射并向后走,或者在跑步时使用 AD 键运行并旋转到我们的速度。

void Update()
{
velocity = new Vector3(Input.GetAxis("Horizontal"), 0,
                       Input.GetAxis("Vertical"));
if (velocity.z > 0.2f)
    {
        forwardVelocityThreshold = true;
    }
    else forwardVelocityThreshold = false;
}

然后在我们的 FixedUpdate() 中检查该布尔值,如果为真,则应用 rotateToVelocity。

调整以下if语句:

    if (ccVelocity != Vector3.zero && forwardVelocityThreshold)
        {
            toRotation = Quaternion.LookRotation(ccVelocity, Vector3.up);
            transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, rotationSpeed * Time.fixedDeltaTime);
        }

现在它按预期工作了,我也按照 Voidsay 在评论中的建议将 deltaTime 更改为 FixedDeltaTime,尽管我听到了关于此的相互矛盾的事情。有人说 FixedUpdate 总是使用 fixedDeltaTime 即使你正在使用 deltaTime,其他人说不,总是在 FixedUpdate 中使用 fixedDeltaTime。


0
投票

我遇到了完全相同的问题,并通过在角色控制器组件中将“最小移动距离”参数更改为“0”设法解决了我的问题。

© www.soinside.com 2019 - 2024. All rights reserved.