车辆抽搐

时间:2019-04-01 19:54:39

标签: c# unity3d

所以我编辑了我的问题,因为我忘记给你看摄像机脚本了。也许问题出在那儿。我只是相信,即使在编辑了旧问题之后,也绝对不会有阅读过编辑文章的人。所以我创建了一个新问题。

所以我有这样的问题。我试图使电车移动。为此,我从Asset Store“ Bezier Path Creator”下载了一个脚本:https://assetstore.unity.com/packages/tools/utilities/b-zier-path-creator-136082。很好用在需要创建第三人称相机之前。在写了一段简短的脚本后,我看到车辆加速后开始抽搐。我坐了几天试图解决这个问题,但我看到了一件事。如果要删除* Time.deltaTime,就不再有抽搐了,但是对FPS的依赖(可以预见的)。同样,如果单击“暂停”按钮并逐帧查看,则不会发生抽动。我该怎么办?

我遇到了一些代码问题:

  switch (acceleration) {
  case -1:
    acc = -12F;
    break;
  case -2:
    acc = -15F;
    break;
  case -3:
    acc = -18F;
    break;
  case 0:
    acc = 0.000f;
    break;
  case 1:
    acc = 9f;
    break;
  case 2:
    acc = 12f;
    break;
  case 3:
    acc = 17.1f;
    break;
  }

  if (Input.GetKeyDown ("e")) {
        acceleration++;
  }

  if (Input.GetKeyDown ("q")) {
        acceleration--;
  }

  if (acceleration < -3) {
        acceleration = -3;
  }

  if (acceleration > 3) {
        acceleration = 3;
  }

if (speed > 40.0f) {
  speed = 40.0f;
  saveSpeed = speed;
  speed = saveSpeed;
} else if (speed >= 0.0f && speed <= 0.03f && acceleration == 0) {
  speed = 0.0f;
} else if (speed <= 0.0f && speed >= -0.03f && acceleration == 0) {
  speed = 0.0f;
} else if (speed <= 40.0f) {
  speed += ((Time.time - startTime)/1000)*(-acc);
}

// Taking a piece of Asset Store code


transform.rotation = pathCreator.path.GetRotationAtDistance(distanceTravelled);
transform.position = pathCreator.path.GetPointAtDistance(distanceTravelled); // I guess the problem is somewhere there

因此,资产商店脚本中有GetRotationAtDistanceGetPointAtDistance个方法:

    public Vector3 GetPointAtDistance(float dst, EndOfPathInstruction endOfPathInstruction = EndOfPathInstruction.Loop)
    {
        float t = dst / length;
        return GetPoint(t, endOfPathInstruction);
    }

    public Vector3 GetPoint(float t, EndOfPathInstruction endOfPathInstruction = EndOfPathInstruction.Loop)
    {
        var data = CalculatePercentOnPathData(t, endOfPathInstruction);
        return Vector3.Lerp(vertices[data.previousIndex], vertices[data.nextIndex], data.percentBetweenIndices);
    }

    public Quaternion GetRotationAtDistance(float dst, EndOfPathInstruction endOfPathInstruction = EndOfPathInstruction.Loop)
    {
        float t = dst / length;
        return GetRotation(t, endOfPathInstruction);
    }

    public Quaternion GetRotation(float t, EndOfPathInstruction endOfPathInstruction = EndOfPathInstruction.Loop)
    {
        var data = CalculatePercentOnPathData(t, endOfPathInstruction);
        Vector3 direction = Vector3.Lerp(tangents[data.previousIndex], tangents[data.nextIndex], data.percentBetweenIndices);
        Vector3 normal = Vector3.Lerp(normals[data.previousIndex], normals[data.nextIndex], data.percentBetweenIndices);
        return Quaternion.LookRotation(direction, normal);
    }

摄像机脚本:

using UnityEngine;
using System.Collections;

public class CameraRotateAround : MonoBehaviour {

public GameObject target;
public Vector3 offset;
public float sensitivity = 3; // чувствительность мышки
public float limit = 80; // ограничение вращения по Y
public float zoom = 0.25f; // чувствительность при увеличении, колесиком мышки
public float zoomMax = 10; // макс. увеличение
public float zoomMin = 3; // мин. увеличение
private float X, Y;

void Start ()
{
    limit = Mathf.Abs(limit);
    if(limit > 90) limit = 90;
    offset = new Vector3(offset.x, offset.y, -Mathf.Abs(zoomMax)/2);
    transform.position = target.transform.position + offset;
    offset.z -= zoom + 3;
}

void Update ()
{
    if(Input.GetAxis("Mouse ScrollWheel") > 0) offset.z += zoom;
    else if(Input.GetAxis("Mouse ScrollWheel") < 0) offset.z -= zoom;
    offset.z = Mathf.Clamp(offset.z, -Mathf.Abs(zoomMax), -Mathf.Abs(zoomMin));

    X = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivity;
    Y += Input.GetAxis("Mouse Y") * sensitivity;
    Y = Mathf.Clamp (Y, -limit, 0);
    transform.localEulerAngles = new Vector3(-Y, X, 0);
    transform.localPosition = transform.localRotation * offset + target.transform.position;
}
}

希望这次有人会帮助我,因为我真的不知道该怎么做才能修复这个袋子。

1 个答案:

答案 0 :(得分:2)

我曾经遇到过类似的问题,This link帮助了我很多。

您遇到的问题是由于相机是第三人称,而不是其跟随对象的父对象。

问题在于,目标对象在移动时会在相机之前移动,从而导致结结,因为相机刚在对象之后移动。尝试将相机脚本放入dplyr中,但是如果这样做不起作用,那么建议您检查一下我上面链接的链接。

该链接讨论的是插值,即在不增加物理时间步长的情况下,将摄像头移动到所提供资产计算摄像头所处的位置,从而消除了结结并实现了平滑运动。检查链接以获取完整信息