- Home /
Question by
conguerror · May 11, 2021 at 03:53 PM ·
c#navmeshagentlocalscaleagentstrafe
Agent Set Destination ZigZag
Update:
Vector3 movementPos = new Vector3(ZigZagDirStrafe().x * strafeMagnitude, transform.position.y, transform.position.z + Random.Range(-1, 2) * forwardMagnitude);
agent.SetDestination(movementPos);
Vector3 ZigZagStrafe:
var t = Mathf.Sign(Mathf.Repeat(randomZigzagTime + (Time.realtimeSinceStartup * 10) / (1000 * zigZagTime), 2.0f) - 1);
var side_wec = transform.right * t;
return (agent.velocity + side_wec).normalized;
The problem is that agent is moving along global X axis and is not strafing right or left locally(according to it's forward). How would I do set destination towards local axis?
Comment
Best Answer
Answer by conguerror · May 12, 2021 at 04:02 PM
[SerializeField]
private Transfrom agentTransform;
// it's better to use separate delta's instead of sharing Time.realtimeSinceStartup
// because you can easily end up with a bunch of enemies oscillating in the same motion
private float zigZagDelta;
private float zigZagDistance;
Vector3 ZigZagStrafe()
{
// using sinus to generate zigzag between -1 and 1 , multiplying with some magnitude
float t = Mathf.Sin(zigZagDelta) * zigZagDistance;
// this is in local space
Vector3 zigZagDisplacementLocal = Vector3.right * t;
// this is now in world space
Vector3 zigZagDisplacementWorld = agentTransform.TransfromDirection(zigZagDisplacementLocal);
return zigZagDisplacementWorld;
}
void Update()
{
zigZagDelta += Time.deltaTime;
Vector3 movementPos = // put the normal movementPos here;
movementPos += ZigZagStrafe(); // add the offset from the zigzag
agent.SetDestination(movementPos);
}