- Home /
Calculate Vector3 based on the other Vector3
Hello fellow programmers!
So in my game I have two virtual joysticks each returning a Vector3 value representing the direction in which the joys are pushed.
One of the joysticks is located on the left , and the second one is on the right side of the screen. Let's call return values accordingly : Left Vector (LV) and Right Vector (RV).
My problem is that the Left Vector needs to be modified based on the input from the right joystick. What I mean is that angle between LV and RV needs to be maintained as it is, and the Right Vector should be treated like Vector3.up by the left joy.
I hope this quick sketch will make it easier to understand :
It is worth noting that the green arrow representing RV in the "HOW IT SHOULD BE" section is there only to show maintained angle between two Vectors. There is no need to physically change the direction of Right Vector.
I only need to get the changed direction of the Left Vector
It is a shot in the dark but maybe I can measure the angle between Right Vector and Vector3.up , then somehow add that angle to the Left Vector ? (I really don't know how it works in Unity)
Is there a function provided by Unity to do such tasks or I am completely wrong and I need a better approach?
Please help me with solving this issue. This is a part of my first kind of a serious project and without your help i may get stuck forever :(
Also, I'm sorry for any spelling mistakes I made since English is shamefully not my first language.
Nicely put together question, sadly i cannot assist you greatly but these links may send you in the right direction.
Finding the angle of a Vector - http://www.dummies.com/education/science/physics/how-to-find-the-angle-and-magnitude-of-a-vector/
Finding the angle between two Vectors - http://www.wikihow.com/Find-the-Angle-Between-Two-Vectors
Dont rely on Vector3 and its shortcuts. Convert them to Vector2.
Without going in to it too much, here's a snippet of code from a Camera-Relative setup.
The angle of the camera (in y) is basically replaced by your Right Stick.
#if UNITY_ANDROID
//ANDROID JOYSTIC$$anonymous$$
if(!Application.isEditor)
{
stickVector = Vector2(moveJoystick.position.x,moveJoystick.position.y);
}
#endif
var playerRotation : float = cameraCurrentT.rotation.eulerAngles.y - thisTransform.rotation.eulerAngles.y;
stickAngle = Vector2.Angle(Vector2(0,1),Vector2(stickVector.x,stickVector.y));
var stickAngleCross : Vector3 = Vector3.Cross(Vector2(0.0,1.0), stickVector);
if (stickAngleCross.z > 0.0)
stickAngle = 360.0 - stickAngle;
var inputRotQuat : Quaternion = Quaternion.AngleAxis((playerRotation),Vector3.up);
var stickTemp : Vector3 = Vector3(stickVector.x,0.0,stickVector.y);
modifierVector = inputRotQuat * stickTemp;
modifiedInput.x = modifierVector.x;
modifiedInput.y = modifierVector.z;
Your answer
Follow this Question
Related Questions
Rotation from another object 1 Answer
Change a property based on angle? 1 Answer
Problem raycasting to quads at certain angles 0 Answers
LookAt formula in Vector3 1 Answer
More precise Vector3 2 Answers