- Home /
need a logical tip for AI
I want object 1 to take object 2 to object 3 as illustrated.
I've tried several methods but they weren't practical because I want it to not depend on the objects positions, also object 2 might be moving.
I don't want a code i just want the logical steps of doing such a thing, just something to put me on the right track.
Thanks in advance :)
Hey, Lets try it once. write a logic depending on the time if time is increasing, initiate object. or else write a logic in transforms.
Hi so you want to make object 1 to move towards object2 and attach it and then move towards object 3 right
it doesn't have to be attached, all i want is to make object 1 move to object 2 and push it towards object 3.
Attaching is a lot easier than pushing. (See my suggested answer below for a solution that attaches.) If it's attached, you can guarantee that object 1 won't drop object 2. If you're pushing using physics, you need to constantly check that object 2 hasn't slid off of object 1's pushing surface.
Thank you, I guess I'll attach them eventually but can I make it so they don't seem attached, for example making the ball rolls when pushed?
Answer by TonyLi · Apr 02, 2013 at 02:11 PM
I was first going to suggest a finite state machine (FSM) attached to object 1 (your AI agent) as a good way to break up the behaviors (get object 2, go to object 3). But you really only need one "goto" behavior.
Let's say you have a component called Goto that has a Target parameter. Attach it to object 1, and set the Target to object 2.
In Update(), Goto can compute a path to its target (object 2) and update its velocity. If you want to get fancy, you can examine the target's velocity to compute an intercept course. How velocity translates to actual motion depends on how you've implemented motion. If you're not doing animation, you can call LookAt() to face the target, and change the transform.position (= velocity * Time.deltaTime) in Update(). This is just a straight move to the target. You'll need to write or use a path finder such as AngryAnt's Path to move around obstacles.
When object 1 reaches object 2, it could trigger a collider on object 2. Object 2's OnTriggerEnter() method would then:
Attach itself to object 1.
Change object 1's Goto target to object 3 (i.e., go to object 3 now).
When object 1 reaches object 3, it could trigger a collider on object 3 that would:
Remove object 2 from object 1.
Change object 1's Goto target to null (i.e., stay here).
So you really only have two components in this system:
A Goto behavior, and
An OnTriggerEnter() handler that updates the Goto target and attaches/detaches a child object.
Answer by fafase · Apr 02, 2013 at 02:20 PM
Mmm what about,
float distance = (obj2.position - obj1.position).magnitude;
// You get the distance between the two and if not claose enough obj1 faces obj2 and then moves towards it
if(magnitude < range){
obj1 looks at obj2
obj1 moves forward
}else{
//At this point we are close enough, we parent the obj2 to obj1 that is now "carrying it around"
obj1 picks up/parent obj2
obj1 has obj2 // here we change a bool to state that we have obj2
}
// if obj2 is carried then we can apply same process towards obj3
if (obj1 has obj2){
obj1 looks at obj3
obj1 moves forward
if(distance with obj3 < range )
end of an exciting game
}
I've thought in parenting but I don't it's a practical way since both of them are rigidbodies.
Your answer
Follow this Question
Related Questions
Artificial Intelligence 1 Answer
Creating an Enemy Jet AI 1 Answer
BDI Agent Programming in Unity3D 1 Answer
My model from Blender does not follow the AI script properly! 2 Answers
Tutorials for AngryAnt's Behave 1 Answer