- Home /
Could anyone explain me this ThirdPerson camera Script?
void LateUpdate(){
Vector3 direction = new Vector3 (0, 0, -distance);
Quaternion rotation = Quaternion.Euler (inputY, inputX, 0);
camTransform.position = playerObj.position + rotation * direction;
camTransform.LookAt (playerObj.position);
}
This script attach to main Camera. I get this script from some place but couldn't understand the: playerObj.position + rotation * direction; . It's like a math kind of thing to me. I just need an explanation to why the position + the rotation equal to some kind of expand circle? Is there any math subject I could take a look at?
looks kinda wonky to me. Why don't you just describe exactly what you're trying to make the camera do and we can help. Are you just wanting the camera to slowly follow the player? or if you simply want the camera fixed behind the player you dont need script, you just make it a child object in the inspector.
I'm not asking for help on code. I just want to understand the code by not being a cargo-cult programmer. Understanding this could open a lot of possibilities to customize and unlock my new skills. Without understanding this, The only type of Camera I could get is the same as GTA but with understanding how it work I could turn it into many cool combination of Camera.
And make child object of Camera seem unprofessional because it's not customization and ...Performance.
Answer by Glurth · Apr 30, 2017 at 05:39 PM
ok, the order of operation is important here
playerObj.position + rotation * direction
..... is equivilent to
playerObj.position + (rotation * direction)
which makes it slightly clearer what's happening..
inside the parens we are multiplying a Vector3 (direction and magnitude) with a Quaternion (an orientation/rotation) When you multiply these together, like that, you are "rotating the vector, by the quaternion" https://docs.unity3d.com/ScriptReference/Quaternion-operator_multiply.html (second half) The resultant vector will have the same magnitude as the original, but its direction will be altered.
The last step is simply Vector3 addition: adding the rotatedDirection to your position. This will generate a point that is offset from your position by the magnitude of the rotatedDirection, in the direction defined by the rotatedDirection Vector3 https://docs.unity3d.com/ScriptReference/Vector3-operator_add.html.
Can you explain more a bout "rotating the vector, by quaternion"? It's a bit hard to grasp what you've said.
The details of how exactly quaternion work is rather complex, but their use is much easier. A Queternion specifies a particular rotation/orientation in 3d space. A Vector3, on the other hand, describes a location (or equivalently, a direction and magnitude) in 3d space.
Let's say as an example, you have a Vector3 that defines a position (1,0,0). And your Quaternion is a 90 degree rotation clock-wise about the Y axis.
When we multiply these two together ("rotating the vector, by quaternion"), the 3d point (1,0,0) gets spun about the Y axis (which passes through the origin at 0,0,0 ) by 90 degrees. The resultant vector in this simple case will be (0,0,1).