- Home /
MoveTowards... a smoother way?
Hi there,
I have a basic AI script for my CPU controlled cars that requires LOADS of waypoints as I am using MoveTowards... which is very linear. I tried Slerp but that just did all kinds of crazy stuff. Is there an easy way to smooth out the transition between waypoints?
Here's my code:
function OnTriggerEnter (other : Collider)
{
if (other.gameObject.CompareTag ("Waypoints")) // Checks if colliding with waypoint via tag
{
print(Waypoints[iIndex]);
// When a waypoint is hit, increase array index so car moves on
if (iIndex < (Waypoints.Length - 1))
{
++iIndex;
}
else
{
iIndex = 0; // Move car back to Waypoint 01
}
}
}
// TODO set up brake zone collision areas
function Update ()
{
if (!LevelSetup.g_bGameIsPaused && LevelSetup.g_bGameInPlay == true)
{
// ACCELERATION
if (!bCarIsBraking)
{
if (fSpeed < fMaxSpeed)
{
fSpeed += fAcceleration;
}
else
{
fSpeed -= fBrakeForce;
}
transform.position = Vector3.MoveTowards(transform.position,Waypoints[iIndex].transform.position,(fSpeed * Time.deltaTime));
}
}
}
Cheers!
Answer by FishBone · Feb 15, 2012 at 04:48 PM
You probably want to calculate a hermite spline.
Found this link on google, have not tried it myself so can't take any responsibility https://www.auto.tuwien.ac.at/wordpress/?p=495
Or you could make your own function using the math on http://cubic.org/docs/hermite.htm
That looks awesome! I think that's pretty much what I need. I'll have a play around today. I always feel a bit guilty about using someone else's code and I do like to try and do it all myself, as I am still learning, but what the hell... that's what it's there for :)
Answer by Owen-Reynolds · Feb 15, 2012 at 05:07 PM
A cheap way is to always move forwards, but smooth-in your rotation. In other words, when you get close to a waypoint, the car starts to turn towards the next one. This is from a working project (which didn't look too bad):
Vector3 dest2 = dest; dest2.y = transform.position.y; // only rotate around y
Quaternion wantFace = Quaternion.LookRotation(dest2 - transform.position);
transform.rotation = Quaternion.RotateTowards(transform.rotation,
wantFace, 90 * Time.fixedDeltaTime);
// 90 is degrees/sec
Then just set velocity or position based only on transform.forward
.
Thanks Owen, the problem is with my game is that I don't really use rotation as it's a simple 2D quad with sprites mapped to it, so it's always facing the camera. I only rotate the GameObject to face the next waypoint so that the math isn't thrown out for changing the textures, depending on the relevant angles to the other player cars. It's a 2.5D SNES-styled "$$anonymous$$ode7" racer... hope that makes it more clear what I'm trying to get across. LOL.
Hmmm, looking at that I'm not sure if I'll be able to slow my cars down to simulate braking into a corner... ah, I'll figure it out. LOL.
If you wanted to use physics but 2D-style graphics, you can create a modelless rigidbody/collider. Your 2D sprite could track the position only (in code,) and rotate on its own, using your current scheme.
Yeah, that might work. I've got it working pretty well at the moment with LOADS of waypoints... gets the job done... but I might have a play anyway.
Your answer
![](https://koobas.hobune.stream/wayback/20220613064112im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Basic AI Locked Axis 1 Answer
Can you help me make this code turn smoothly? 0 Answers
How do I moveTowards all the items in a list one at a time? 1 Answer
Error with Rotating script 2 Answers