Having trouble getting this right.
ball.AddTorque (joystick.MoveInput () * speed, ForceMode.Impulse);
The joystick.MoveInput () provides a Vector3.
I need to apply the torque (vector3) based on the current orientation of the camera. When I try multiplying the vector3 of the camera it gives me an error… can’t multiply vector3’s(?)
Any help would be greatly appreciated!
Sorry for the lste reply ^^. I saw this question and had it still open in my firefox session (along with 20 others…) but haven’t managed to answer it until now.
Let’s solve the question bottom to top. You can multiply two vectors together, but there are actually multiple ways of doing so. Imagine we have two vectors “A” and “B”:
- Vector3.Scale(A,B) does simply this: new Vector3(A.xB.x, A.yB.y, A.z*B.z); So it simply multiplies component by component. This is actually a quite rarely used multiplication because when it comes to directions or position vectors the resulting vector doesn’t have much meaning.
- Vector3.Dot(A,B) does something similar to scale but it returns a single number, not a vector. The dot-product looks like this: (A.xB.x + A.yB.y + A.z*B.z). This single number has an important relation to the vectors. The number represents the length of A times the length of B times cosine of the angle between the two vectors. This is very useful to check if a vector is perpendicular to the other, if it points in the same or opposite direction, can be used to determine the angle between those vectors and allows to project one vector onto the other.
- Vector.Cross(A,B) calculates the cross-product. This is a very special product which actually returns another vector. However the resulting vector will be perpendicular to A and B. So it will be a normal vector for the plane A and B represent. The length of the result also equals the area of the parallelogram. Each component of the result is made up by cross multiply the other 2 components. So x = A.yB.z - A.zB.y likewise y = A.zB.x - A.xB.z and z = A.xB.y - A.yB.x. The direction of the result depends on the order in which you multiply A and B together. You can use the right-hand-rule / left-hand-rule depending on the type of coordinate system you work in. Usually the right-hand-rule applies but in Unity, since we have a left-handed system, we have to use the left-hand-rule.
However all those multiplications aren’t useful for your case. To me it sounds like your input vector is in local space while “AddTorque” expects a vector in world space. So you either have to:
- transform your local vector into world space by using ball.TransformDirection() and pass it to AddTorque
- or simply use AddRelativeTorque instead which expects a vector in local space.
I’m not sure what your speed vector represents. If you want to uniformly scale your input vector you should multiply your vector with a single “speed” value and not a vector.
Also keep in mind that “ForceMode.Impulse” is ment for one-time-changes. Think about billard when two balls collide the transfer an impulse (both linear motion and torque). If you want to apply a torque over multiple frames you should use ForceMode “Force” or “Acceleration”. Also keep in mind that “Impulse” and “Force” calculate the amount of torque applied based on the mass of the objects while VelocityChange and Acceleration are independent of the mass.