New answers tagged

2

Here's a Unity-specific option in the same vein as sam hocevar's answer: Vector3 GetRandomUnitPerpendicular(Vector3 v) { float angle = Random.Range(0, Mathf.PI * 2f); // Generate a uniformly-distributed unit vector in the XY plane. Vector3 inPlane = new Vector3(Mathf.Cos(angle), Mathf.Sin(angle), 0f); // Rotate the vector into the plane ...


0

You are looking for any vector that is completely in the plane described by a normal vector. Let's call your normal vector n, and the result vector (what you want to calculate) r. You know something about the relationship between the two vectors, namely that their dot product must be 0. That's one equation, but you have 3 unknowns here (the x, y, and z ...


0

We know that the cross product of two vectors in 3-dimensional space is another vector which is orthogonal to both original vectors. Given a vector which starts at the origin, we can just pick any random vector which starts at the origin to do the cross-product against. Since the cross-product produces a vector which is orthogonal to both, and we picked a ...


1

I would do the following: have your vector u (ensure it’s normalised) pick an orthogonal vector v using any existing method (ensure it’s normalised) pick a random angle α a good random unit vector is therefore: v·cos(α) + (u × v)·sin(α) If you wish your vector to also have random length, you can then multiply it by a random number, or possibly by the ...


0

What is happening is that your "nextPoint" vector is not actually describing a point in world space; it is describing a direction and distance away from your starting point. So in order to get the world space position of where this vector is pointing to, take the world space position of your starting point, then add your offset vector to it: nextPoint = ...


0

You could add a child to the barrel and add a script to the child that will set one of its rotation axis to the starting value which means one of its rotation axis eg. forward will always stay the same and it could act as an indicator of the forward direction of the barrel since it will be moving and rotating on other two axis the same as the barrel. Your ...


1

You can use the dot product trick to get a list of instances actually visible by the player (thus lying inside the camera frustum), and check for the distance from your camera object to get the closest one. First, you can test a certain number of instances so that their distance is low enough for them to be considered "near" the camera (or the player). ...


1

As mentioned in the comments you probably want a Raycast. An example of how to implement a forward-facing raycast in a Fixed Update cycle is spelled out exactly in the Unity Scripting API Documentation here. This will return true if the raycast strikes a collider in front of whatever object you attach the script to. From the documentation above: using ...


3

Assuming that you're making a 3D game, you can use Physics.OverlapSphere to get a an array of Colliders that are within a certain range of the player. To check if the object is "visible" to the player, you could simply call Physics.Raycast in the direction of the object(s) returned by OverlapSphere and check if nothing that isn't the object you're checking ...


0

Typically, to get the closest object from the player, you do something like this (generic pseudo-code): var listOfObjects = getAllObjects() var closestObject = null var distanceSquareOfClosest = infinite for otherObject in listOfObjets: if otherObject != this: var distanceSquareOfThisObject = (otherObject.position - this.position).lengthSquare() ...


1

So I guess you could save like the last 10 rotations in a List and get the average of them with this function. The function return could be set as your final rotation. This should smoothen your rotation. private Quaternion calcAvg(List<GameObject> rotationlist) { if (rotationlist.Count == 0) throw new ArgumentException(); float x, y, ...


1

For posterity...it ended up being a simple handedness issue, but only around the Y (pitch) axis. I had it stuck in my head that positive pitch increases Z and negative pitch decreases Z, which isn't true in a right-hand system. Lesson learned!


2

Both XNA and Fabian Giesen’s implementation behave correctly. Transforming vector (1, 0, 0) by quaternion (w=0.6532815, x=-0.270598, y=0.270598, z=0.6532815) does resut in (0, 0.7071, -0.7071). It also does in Unreal Engine, in my own quaternion implementation, as well as in Wolfram Alpha. Wolfram Alpha also gives the 3×3 matrix equivalent of the ...


-1

Simple: Unlike the rest of these answers. V1*sin(dir) - V2*sin(dir) 30*sin(090) - 40*sin(270) = 70 If you want the range, just add it in like so Range-(30*sin(090) - 40*sin(270) = 70)


3

Instead of foreach, you'll want to use for. for (int i = 0; i < position.Count; i++) { Vector3 posit = position[i]; ... } This way, you'll be able to access the previous position using position[i-1]. Keep in mind, however, that this won't work if i == 0 because you can't go less than zero, so you'll want to add some checks to ensure i > 0 ...


0

Looks like you need to restrict the euler angles of the turret, after the quaternion rotation has been effected. basically you'd just do something like: transform.localEulerAngles = new Vector3(transform.localEulerAngles.x < 0 ? 0 : transform.localEulerAngles.x, transform.localEulerAngles.y, transform.localEulerAngles.z); ...I think X is the right axis ...



Top 50 recent answers are included