- Home /
Move navmesh agent in random direction within range
So right now my navmesh agents just move to a random point by using:
Vector3 randompoint = this.transform.position + UnityEngine.Random.insideUnitSphere * WalkRadius;
NavMeshHit hit;
NavMesh.SamplePosition(randompoint, out hit, WalkRadius, 1);
Vector3 finalPosition = hit.position;
However that's a little bit to random. I want him to move in a direction of in like a range of -90, 90 angle from the angle he is currently facing. And a random distance away from him like 0, 30 units.
it's kind of hard to explain.
I have no idea how to accomplish this, I have a hard time understanding the things you can do with a vector3. I know to get direction of 2 objects you just do like: Vector3 dir = transform.position - target.position; and I do not even fully understand how that works, like I can use it but I don't really know whats going on.
----edit---
Messed around a bit and did some research and got this:
void Update()
{
if (Input.GetKeyDown(KeyCode.H))
{
Vector3 targetDir = Quaternion.AngleAxis(Random.Range(-30.0f, 30.0f), transform.up) * transform.forward;
targetDir = targetDir.normalized * 25;
GameObject cube = Instantiate(Cube);
cube.transform.position = targetDir;
}
}
which it works it always spawns a cube 25 units away on a random angle relative to where the player is looking... however it ALWAYS spawns the cube 25 units away from the middle of the map, not from where the player is at.
Answer by Smurfj3 · Aug 21, 2016 at 10:09 PM
Fixed it myself I forgot to do the following: targetDir = transform.position + targetDir.normalized * 25;
before I just had: targetDir = targetDir.normalized * 25;
Your answer
Follow this Question
Related Questions
Random /Organic/ Weapon Inaccuracy 1 Answer
Making player face the direction it moves (using angles). 1 Answer
Object falling from random direction towards the player?? 0 Answers
Shaderlab; stripes shader - make stripes at arbitrary angle 0 Answers
How to get offset postition after rotation ? (Spent hours figuring it out) 3 Answers