Reputation: 388
So I edited my question because I have forgotten to show you a camera script. Maybe the problem is somewhere there. I just believe that even after editing the old question there will absolutely no people who read edited article. So I've created a new question.
So I have such a problem. I try to make a tram moving. To do it I downloaded a script from Asset Store "Bezier Path Creator": https://assetstore.unity.com/packages/tools/utilities/b-zier-path-creator-136082. It works nice. Before the need of creating third person camera. After writing a short script I saw that vehicle is twitching after getting some speed. I sat a couple of days trying to solve this problem and I saw one thing. If to remove * Time.deltaTime
there are no twitching anymore but there is dependence on FPS (what was predictable). Also if to click Pause button and look frame by frame there are no twitching. What can I do about it?
There are some pieces of code I have problems with:
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
So there are GetRotationAtDistance
and GetPointAtDistance
methods from Asset Store script:
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);
}
Script of camera:
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;
}
}
Hope somebody'll help me this time because I really don't know what can I do to fix this bag.
Upvotes: 1
Views: 248
Reputation: 131
I once had a similar problem and This link Helped me out a ton.
The problem you are experiencing is due to The camera being 3rd person and not parented to the object it is following.
The problem is that the target object when moved, moves before the camera, thus causing the stutter, as the camera moves just after the object. Try putting the camera script in LateUpdate
, but if that does not work, then I suggest checking the link I have linked above.
The link talks about interpolation, which, without increasing your physics timestep, moves the camera to a position which the provided asset calculates the camera will be at, thus removing the stutter and achieving smooth motion. Check the link for the full info
Upvotes: 2