News › Forums › RAIN › Sample Projects, How To, and Code › Just for fun - Vehicles in RAIN
This topic contains 4 replies, has 2 voices, and was last updated by prime 1 year, 9 months ago.
-
AuthorPosts
-
November 6, 2022 at 5:16 pm #33977
Just messing around, I created a really simple custom motor that let’s me control vehicles in RAIN. This isn’t perfect, but it’s a start. Best used either without a navmesh, or with vehicles that can make tight turns. (To go with no navmesh, just add a graphtag to the vehicle’s RAIN Navigator and set it to nograph.)
using UnityEngine; using System.Collections; using RAIN.Core; using RAIN.Motion; using RAIN.Serialization; [RAINSerializableClass, RAINElement("Vehicle Motor")] public class VehicleMotor : BasicMotor { public override void ApplyMotionTransforms() { AI.Kinematic.Velocity = AI.Body.transform.forward * AI.Kinematic.Velocity.magnitude; base.ApplyMotionTransforms(); } }
November 6, 2022 at 5:17 pm #33978Just FYI - with my test vehicle I set the Face Before Move Angle to 360 and the Rotation Speed to 60 on the motor. Allow Off Graph Movement = true.
November 7, 2022 at 2:05 pm #33993Added a little simulated momentum. This is just for a little toy car, so simple works.
using UnityEngine; using System.Collections; using RAIN.Core; using RAIN.Motion; using RAIN.Serialization; [RAINSerializableClass, RAINElement("Vehicle Motor")] public class VehicleMotor : BasicMotor { public override void ApplyMotionTransforms() { float priorSpeed = AI.Kinematic.PriorSpeed; float currentSpeed = AI.Kinematic.Speed; float newSpeed = Mathf.Lerp(priorSpeed, currentSpeed, AI.DeltaTime * 2f); AI.Kinematic.Velocity = AI.Body.transform.forward * newSpeed; base.ApplyMotionTransforms(); } }
November 7, 2022 at 8:42 pm #33997Thanks for providing this example Prime!
If I used this with a vehicle that used a navmesh (so that they can avoid obstacles while chasing the player), is it somehow possible to have the vehicle momentum cause it to “overshoot” navpoints so it seems to be sliding through them as it attempts to maintain its line… please see how the enemy vehicles move in the clip below.
In other words, I don’t want the vehicles navigating directly from navpoint to navpoint like they are on rails, but slipping and sliding around like they are driving in RAIN (har har) or almost behaving like boats (would be good for gun boat AI too, come to think of it). I need to start going over some RAIN tutorials and play around a bit. Thanks for your help!
November 10, 2022 at 9:14 am #340071) To get the vehicle physics right, I’m sure you’ll need a more complex motor. Either one that uses the built-in physics system, or one that simulates momentum and sliding/drifting.
2) To use the navmesh, you will probably need a custom navigator that takes into account speed, momentum, and path curvature when calculating paths. It will also need to calculate the GetNextPathWaypoint call differently - using something more like regular SteeringBehaviors vs. what the BasicNavigator does.
There would be a bit of work to get it right, but it isn’t impossible. I probably won’t have time to try it myself for a while, but certainly let us know if you do and we can try to help.
-
AuthorPosts
You must be logged in to reply to this topic.