News Forums RAIN Sample Projects, How To, and Code Just for fun - Vehicles in RAIN

Tagged: , ,

This topic contains 4 replies, has 2 voices, and was last updated by  prime 2 years, 1 month ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #33977

    prime
    Keymaster

    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();
        }
    }
    #33978

    prime
    Keymaster

    Just 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.

    #33993

    prime
    Keymaster

    Added 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();
        }
    }
    #33997

    santokki
    Participant

    Thanks 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!

    #34007

    prime
    Keymaster

    1) 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.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.