- Home /
How do I programmatically specify direction from a given vector?
I'm hoping this is incredibly easy, but I've been smashing my head against a wall over this without much luck- let's say I have three variables:
Vector3 PlayerLocation; //Represents current player location
float spawnDirection; //Always clamped between 0-360
float spawnRange; //Represents desired distance from player at which objects should spawn
What I'm trying to do is get a Vector3 representing a transform spawnRange distance from PlayerLocation in spawnDirection, where spawnDirection=0 corresponds to player.forward, 90 corresponds to player.right, and so forth. Is there an easy way to do this? I'm half-suspicious that the math is trivial, but I can't quite suss out how to do it.
I feel like an absolute moron, thank you so much for explaining it clearly! :) $$anonymous$$y big block was that I wasn't thinking in terms of adjusting magnitude, I was stuck on "$$anonymous$$ultiply something by something, and you get something in the correct range". Thank you, this has been befuddling me all day :)
Answer by hexagonius · Jul 14, 2015 at 02:38 PM
since the player has a transform you know its forward. You can then do Vector3 spawnVector = player.transform.forward * Quaternion.Euler(0,spawnDirection,0);
This rotates the vector about the given degrees. Then adjust the magnitude: spawnVector *= spawnRange;
And then get the final SpawnPosition: Vector3 spawnPosition = player.transform.position + spawnVector;