Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unity3d 在update()方法中,如何一致地更新对象的位置?_Unity3d - Fatal编程技术网

Unity3d 在update()方法中,如何一致地更新对象的位置?

Unity3d 在update()方法中,如何一致地更新对象的位置?,unity3d,Unity3d,我们正在使用Verlet方法移动Tukey。但它只移动一次,而不是连续移动。更新火鸡位置的代码在Update()方法中,因此它应该在每一帧中运行。但它只运行了一次 此外,我们在更新方法中添加了三倍于更新Turkey Line渲染对象位置的代码,看起来Turkey的新位置只移动了一次 using System.Collections; using System.Collections.Generic; using UnityEngine; public class Generate

我们正在使用Verlet方法移动Tukey。但它只移动一次,而不是连续移动。更新火鸡位置的代码在Update()方法中,因此它应该在每一帧中运行。但它只运行了一次

此外,我们在更新方法中添加了三倍于更新Turkey Line渲染对象位置的代码,看起来Turkey的新位置只移动了一次

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

public class GenerateTurkeys : MonoBehaviour
{

    public LineRenderer lineRenderer;
    // Start is called before the first frame update
    //int numberOfTurkeys;
    static int NUM_PARTICLES = 26;
    float fTimeStep;
    Vector3[] m_position = new Vector3[NUM_PARTICLES];
    Vector3[] m_acceleration = new Vector3[NUM_PARTICLES];
    Vector3[] m_oldPosition = new Vector3[NUM_PARTICLES];
    Vector3[] m_newPosition = new Vector3[NUM_PARTICLES];



    void Start()
    {
        lineRenderer = gameObject.GetComponent<LineRenderer>();

        lineRenderer.GetPositions(m_position);

        for(int i = 0; i < m_acceleration.Length; i++)
        {
            m_acceleration[i] = new Vector3(0.0f, -9.8f, 0.0f);
        }

        fTimeStep = 5.5f;


    }

    // Verlet integration step void ParticleSystem::
    void Verlet()
    {
        var random_direction = Random.Range(-1, 1);


            for (int i = 0; i < NUM_PARTICLES; i++)
            {
                m_newPosition[i] = 2 * m_position[i] - m_oldPosition[i] + m_acceleration[i] * fTimeStep * fTimeStep;
                m_oldPosition[i] = m_position[i];
                                }





    }



    // Update is called once per frame
    void FixedUpdate()
    {


        Verlet();
        lineRenderer.SetPositions(m_newPosition);

          }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类GenerateTurkey:单行为
{
公共线条渲染器线条渲染器;
//在第一帧更新之前调用Start
//国际火鸡;
静态int NUM_粒子数=26;
浮动fTimeStep;
Vector3[]m_位置=新Vector3[NUM_粒子];
Vector3[]m_加速度=新Vector3[NUM_粒子];
Vector3[]m_oldPosition=新矢量3[NUM_粒子];
Vector3[]m_newPosition=新矢量3[NUM_粒子];
void Start()
{
lineRenderer=gameObject.GetComponent();
lineRenderer.GetPositions(m_位置);
对于(int i=0;i
首先,
FixedUpdate
由物理引擎使用,更新方式不同于常规的
Update
方法。除非你想做的事情必须与物理引擎同步,否则你应该使用
Update

其次,您的
m_位置
向量永远不会更新,您只能在
Start
方法中调用
lineRenderer.getPositions
。因此,您的
m_old positions
将始终保持不变,并且位置不会改变。要更正此问题,您的
Verlet
方法还应在计算新位置后更新
m_位置
向量。 大概是这样的:

    void Verlet()
    {
        var random_direction = Random.Range(-1, 1);


            for (int i = 0; i < NUM_PARTICLES; i++)
            {
                m_newPosition[i] = 2 * m_position[i] - m_oldPosition[i] + m_acceleration[i] * fTimeStep * fTimeStep;
                m_oldPosition[i] = m_position[i];
                m_position[i] = m_newPosition[i];
            }
}

void-Verlet()
{
var random_方向=随机范围(-1,1);
对于(int i=0;i