- Home /
Moving Navmesh agents backward
Hello,
I'm trying to move some Navmesh agents backward and i'm getting unsuccessfull for now. The animation is good but my agent is going forward with this backward animation and since we can't put a negative value for speed agent i wonder... How that can be done? What am I missing? Thanks in advance.
Put the visuals as a child of the NavigationAgent and rotate the visuals on the y axis 180 degrees.
I tried that method earlier but i dont' want my NPC to rotate around so that doesn't seems to be a nice way unless i'm missing something...
Right now i think I found a better solution (but it's not done yet) using agent.updateRotation=false and then do the rotate myself. I will tell later if it's a good way.
You don't have to use navmesh you can use any other method to move your character backwards. Of course it wouldn't check for walls etc as navmesh would.
Answer by barbe63 · Jan 13, 2015 at 09:24 AM
Yes!
I found it so for those who are interested, i'm using:
agent.updateRotation=false; // disable the automatic rotation
while(condition)
{
Vector3 direc = Vector3.zero;
direc.y = AngleTo360(CalculateYAngle(trans.position, agent.steeringTarget)); // this return an angle between my NPC and the next waypoint of agent from 0 to 360 degrees
//then i need to prevent the difference from beeing too big (in case one is at 10 and the other 350 for example)
if (Mathf.Abs(direc.y-trans.eulerAngles.y)>180)
{
if(direc.y<180)
direc.y+=360;
else
direc.y-=360;
}
trans.eulerAngles = Vector3.Lerp(trans.eulerAngles, direc, Time.deltaTime);
yield return null;
}
agent.updateRotation=true; //when no longer need to step back then go to normal
Of course if someone has a more efficient way to do this i'm buying :p
Answer by sysmaya · Mar 02, 2017 at 10:42 AM
Disable agent.
Enable animation walkBack.
use transform. translate for move.
Answer by Z0leee87 · Dec 11, 2021 at 05:55 PM
I am a beginner, but i think there is an option for this:
// from WORLD to LOCAL
Vector3 inversePos = transform.InverseTransformPoint(inFrontOfMe.transform.position);
// DEF COORDS IN LOCAL
float locTargetOnX = inversePos.x;
float locTtargetOnZ = inversePos.z;
// NEGATIVE (local) COORDS
float negLocTargetOnX = -locTargetOnX;
float negLocTtargetOnZ = -locTtargetOnZ;
// FROM LOCAL TO WORLD (works only flat plane because of the Y (I would be glad if you could help))
Vector3 negativePos = transform.TransformPoint(negLocTargetOnX, 0.0f, negLocTtargetOnZ);
// SET THE OTHERS
myAgent.speed = 2.0f;
//myAgent.SetDestination(new Vector3(0,0,0));
myAgent.SetDestination(negativePos);
myAgent.isStopped = false;
Sorry for my english...