当我尝试转换此播放器控制器时,它只会重置为原始位置 [英] When I try to transform this player controller it just resets to the original position

查看:18
本文介绍了当我尝试转换此播放器控制器时,它只会重置为原始位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我现在使用的播放器控制器的简化版本,它仍然会产生下面解释的错误:

This is a reduced version of the player controller I am using now, which still produces the error explained below:

using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
using UnityStandardAssets.Utility;
using Random = UnityEngine.Random;

namespace UnityStandardAssets.Characters.FirstPerson
{
    [RequireComponent(typeof (CharacterController))]
    public class FirstPersonController : MonoBehaviour
    {
        [SerializeField] private float m_RunSpeed;

        private CharacterController m_CharacterController;

        // Use this for initialization
        private void Start()
        {
            m_CharacterController = GetComponent<CharacterController>();
        }

        private void FixedUpdate()
        {
            Vector3 move = Vector3.right * m_RunSpeed;
            m_CharacterController.Move(move * Time.fixedDeltaTime);
        } 
    }
}

我正在使用标准资产脚本,并且我正在尝试让脚本将玩家传送到不同的地方.当我尝试移动播放器时,它会移动到那个位置一帧,然后又回到它的位置.

I am using the standard assets script and I am trying to have a script teleport the player to different places. When I try and move the player it goes to that position for a frame, then goes right back to its position.

player.transform = new Vector3(1,2,3);
// works as expected, but then next frame, player's
// position is back to where it was before

推荐答案

出现这个问题是因为 Move 可能无法读取 transform.position<的最新值/代码>.

This problem occurs because Move may not be able to read the up-to-date values for transform.position.

此问题已在官方 Unity 问题跟踪器上报告 此处,并且在此处也发布了解决方案:

This problem has been reported on the official Unity Issue Tracker here, and a solution was posted there as well:

这里的问题是在物理设置中禁用了自动同步变换,所以 characterController.Move() 不一定知道变换设置的新姿势,除非 FixedUpdatePhysics.Simulate() 调用发生在 transform.position 和 CC.Move() 之间.

The problem here is that auto sync transforms is disabled in the physics settings, so characterController.Move() won't necessarily be aware of the new pose as set by the transform unless a FixedUpdate or Physics.Simulate() called happened in-between transform.position and CC.Move().

要解决这个问题,要么在物理设置中启用自动同步变换,要么在调用 Move()

To fix that, either enable auto sync transforms in the physics settings, or sync manually via Physics.SyncTransforms right before calling Move()

因此,您可以通过编辑 FixedUpdate 来解决您的问题,使其在 Move 之前调用 Physics.SyncTransforms,如下所示:

So, you could fix your problem by editing FixedUpdate so that it calls Physics.SyncTransforms before Move, like this:

using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
using UnityStandardAssets.Utility;
using Random = UnityEngine.Random;

namespace UnityStandardAssets.Characters.FirstPerson
{
    [RequireComponent(typeof (CharacterController))]
    public class FirstPersonController : MonoBehaviour
    {
        [SerializeField] private float m_RunSpeed;

        private CharacterController m_CharacterController;

        // Use this for initialization
        private void Start()
        {
            m_CharacterController = GetComponent<CharacterController>();
        }

        private void FixedUpdate()
        {
            Vector3 move = Vector3.right * m_RunSpeed;

            Physics.SyncTransforms();
            m_CharacterController.Move(move * Time.fixedDeltaTime);
        } 
    }
}

这篇关于当我尝试转换此播放器控制器时,它只会重置为原始位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆