I have to randomize vector3 between -variable and variable in X and Y axis. So Z axis is forward and I need find random vector3 in a circle around that vector, but that all in local space. Creating vector3 and adding it to local space is a bad idea.
Player can walk and rotate around Y axis and his face can move around X axis.
Asked
Active
Viewed 1,500 times
-1

Olivier Moindrot
- 27,908
- 11
- 92
- 91

Konrad
- 21,590
- 4
- 28
- 64
-
2I can not understand what do you want to achieve. – Skyblade Sep 14 '15 at 15:11
-
1Check out [`TransformVector`](http://docs.unity3d.com/ScriptReference/Transform.TransformVector.html) and friends. They can help you transform your random vector between spaces. As your question stands it's very unclear which spaces you want. (And there are already plenty of answers for how to make random vectors.) – 31eee384 Sep 14 '15 at 15:14
-
Added picture, is it clear enought right now? – Konrad Sep 14 '15 at 15:46
-
1It makes more sense, but "Creating vector3 and adding it to local space is a bad idea." doesn't. It seems like that's exactly how you'd go about this: find the point in local space and transform it to world space if you have to (and it's still not clear what space you want the point in). – 31eee384 Sep 14 '15 at 16:00
-
When you create vector3 in world space and than change it's variables to local spaces it will have the same position but presented in other values. Or I don't know, maybe I should record where my problem is or something. – Konrad Sep 14 '15 at 16:29
-
1@Konowy That's a true statement, but I don't see why it's a problem--in fact, it's the solution! I still fail to see what question you have. – 31eee384 Sep 14 '15 at 19:33
1 Answers
1
is this what you want?
var cam = Camera.main;
var distance = 1000f;
var tolerance = 1f;
var offset = cam.transform.forward * distance;
var direction = (offset + new Vector3(Random.Range(0f, tolerance), Random.Range(0f, tolerance), 0f)).normalized;
var lineOfSight = new Ray(cam.transform.position, direction);

AustinWBryan
- 3,249
- 3
- 24
- 42

maraaaaaaaa
- 7,749
- 2
- 22
- 37
-
-
@LOLslowSTi I think Konowy doesn't have a first-person view, and expects the ray to come from the character's forward vector. All you should have to do to adopt this is remove each `cam.` to use the current object's `transform`. – 31eee384 Sep 14 '15 at 19:35
-
It's turned out that I'm an idiot and everyone else was correct. I was just creating vector in wrong way. – Konrad May 02 '17 at 16:28