- Home /
My "Smooth Rotation" Jerks around randomly when switching between points.
My character rotation likes to jerk to the right every time it switches points!
What I want is to get my car to constantly move between a set of points smoothly and at a consistent speed. It moves between the points correctly, but the rotation changes to an odd point everytime.
It always rotates a little up and a little to the right every time it switches points. I would like my answer in C# please. While I could probably convert code from Javascript to C#, I hate Javascript, so I never use it. Here is my code:
using UnityEngine; using System.Collections;
public class ParentMovement : MonoBehaviour {
[SerializeField]
private Transform[] points;
[SerializeField]
private float speed;
[SerializeField]
private float rotationSpeed;
private int i = 0;
// Update is called once per frame
void Update ()
{
float step = speed * Time.deltaTime;
if (i < points.Length)
{
//Rigidbody GET IT
Rigidbody rb = GetComponent<Rigidbody>();
//Move the location to the transform.position.
transform.position = Vector3.MoveTowards(transform.position, points[i].position, step);
//Smoothly look to the other object
Vector3 direction = (points[i].position - transform.position);
Quaternion lookRotation = Quaternion.LookRotation(direction);
//Actually LOOK
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * rotationSpeed);
//Change the points.
if(transform.position == points[i].position)
{
i++;
}
}
}
}
Watch out for comparing floating point values as you are doing here:
if(transform.position == points[i].position)
They're unlikely to match most of the time. To start, try changing the points based on when you're close enough. This has one of the more simple explanations out there: dotnetperls
Your answer
Follow this Question
Related Questions
i want to make a path system like in racing in gtav,i want to make a path system like racing in gta 0 Answers
Path system or Predetermined paths for Camera Fly-throughs / Ladder climbing / etc 1 Answer
Need to change direction of transform? 2 Answers
A* Pathfinding, destroying a path once it has reached its end 0 Answers