News › Forums › RAIN › Sample Projects, How To, and Code › Manual Movement in Custom Action
Tagged: AI, custom movement, kinematic
This topic contains 2 replies, has 2 voices, and was last updated by Sigil 1 month, 2 weeks ago.
-
AuthorPosts
-
August 19, 2022 at 8:40 pm #38955
Hello,
I’m working with RAIN to try to develop a melee combat AI. So far it’s going well, but now one of the animations involves the character leaping forward in a strike.
The setup is that the animator does not apply root motion, so the animation that plays doesn’t move (this is intended).
What I would like to do is create a custom action that will move the character from the origin point (recorded during the Start function of the action), to the destination point (a value pulled from AI working memory), over a given number of seconds.I’ve managed to get everything working save for the actual motion. The AI Rig is using a character controller motor, and I have tried a few things, involving using the Character Controller’s Move function, setting the AI motor’s destination and attempting to move it that way, and finally the segment below which is attempting to outright set the GameObject’s position:
public Expression amount; public Expression seconds; private float totalTime; private float elapsed; private Vector3 start; private Transform end; public override void Start(RAIN.Core.AI ai) { base.Start(ai); totalTime = seconds.Evaluate<float>(ai.DeltaTime, ai.WorkingMemory); start = ai.Body.gameObject.transform.position; end = ai.WorkingMemory.GetItem<GameObject>("attackTarget").transform; } public override ActionResult Execute(RAIN.Core.AI ai) { elapsed += ai.DeltaTime; float pct = elapsed / totalTime; ai.Body.transform.position = Vector3.Lerp(start, end.transform.position, pct); if (elapsed < totalTime) return ActionResult.RUNNING; return ActionResult.SUCCESS; } public override void Stop(RAIN.Core.AI ai) { base.Stop(ai); }
No matter what I try, the character doesn’t move during the calls to this action. Any help you can provide would be greatly appreciated!
August 19, 2022 at 8:44 pm #38956I figured it out less than 5 minutes after posting this…turns out instead of
ai.Body.transform.position = Vector3.Lerp(start, end.transform.position, pct);
I needed to use
ai.Kinematic.Position = Vector3.Lerp(start, end.transform.position, pct);
I’ll leave this here in case someone else runs into the same problem!
August 20, 2022 at 12:58 pm #38962Glad you got it working.
Just to give a little more detail: internally RAIN works with the Kinematic instead of the actual position of the AI, and the values get copied back and forth at the beginning and end of the Update. This is a holdover from the original RAIN setup that used steering behaviors, which involved several passes of adding in velocities before the end of the Update function where it would be applied to the AI position. We haven’t moved away from it yet as it still provides a lot of usefulness for other things. For example it handles all of our local/world transformation that we use for navigating parented AI across parented Navigation Meshes.
-
AuthorPosts
You must be logged in to reply to this topic.