- Home /
AI Mecanim Movement help!!
How would I replace movement direction with one that is not from Input.
As in how would I convert this code into one that the AI can use. As in how can I tell it that h is movement on the horizontal axis and v is vertical?
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
This is the what the animator uses in mecanim. Does this have to change too?
anim.SetFloat("Speed", v);
anim.SetFloat("Direction", h);
So what I need is how to tell the dam thing that float h and v is movemt on horizontal axis and vertical axis.
Is it float h = transform.position.x? I tried that and it just keeps strafing right.
I hope I was clear enough. Please help. Thanks.
Answer by Chronos-L · Aug 01, 2013 at 02:02 AM
If you have a target, you can use some simple calculation to figure out the motion of your AI.
public Transform target;
...
//A quick way to figure out how fast you need to move by
//checking the distance between your AI and its target
float speed = Vector3.Distance( target, transform ) * ...;
float direction = ...;
anim.SetFloat("Speed", speed);
anim.SetFloat("Direction", direction);
Ah ok so I got for speed. Still confused a little.
Vector3.Distance(target.position, transform.position) * The movespeed is governed by the animation so nothing here right?.
The guy now walks forward?
Now I got confused I have no idea about the direction? Sorry I'm kinda dumb.
He constantly walks forward as in I have not said walk forward.
That can be fixed with if statement though right? As in ?
if(something){
anim.setfloat("Speed", speed);
}
A direction is not a single float, unless it is in degrees or radians. What is 'direction'? Convert your (h,v) vector into the same measure 'direction' uses.
@Dead$$anonymous$$enny
That can be fixed with if statement though right? As in...
You need to know that you are writing codes for an AI. When Input
is received from the player, the player have made decisions on a number of things:
Where should I go?
Which way should I pick?
Is there any enemy?
Should I ...
You need to incorporate these complex thought process into your code, in order to get a good AI. You can't just insert one line and it will just work. But, you are in the right direction, just keep on adding more and more conditions and decision making and you should get where you want to.
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Mecanim Making an NPC walk 1 Answer
Nav Mesh Agent break between Rotation changes 1 Answer
Basic AI Movement C# 3 Answers