- Home /
Camera Limitations Using Inverse Vectors
My code works fine with some world orientations but somehow it doesnt works with other orientations. I have a camera , camera target pivot point and camera offset borders along to the that pivot point.
The camera must be able to move freely within the boundaries of the red line.
Here is my scene setup
YELLOW DOTS Camera Position Points GREEN DOTS Camera Look Rotation Points RED DOTS Camera Borders
Number 1,2 charts works fine as i expected camera doesnt go out of that red borders but the other ones 3-4 camera somehow will stuck on z or x axis .
Here is my camera movement code .
CAMERA MOVEMENT
private void MoveCamera(Vector3 force, float sens)
{
Chart currentChartObj = currentChart.GetComponent<Chart>();
Vector3 desiredForce = new Vector3(force.x,force.y,0) * Time.deltaTime * sens;
Vector3 currentNormalPos = currentChart.transform.position + currentChart.transform.TransformDirection(currentChartObj.positionOffset);
cameraTransform.transform.Translate(desiredForce);
Vector3 currentTempPos = cameraTransform.transform.position;
float finalX = Mathf.Clamp(currentTempPos.x, (currentNormalPos + currentChart.transform.InverseTransformVector(-Vector3.right * currentChartObj.cameraBorderOffsets.x)).x, (currentNormalPos + currentChart.transform.InverseTransformVector(Vector3.right * currentChartObj.cameraBorderOffsets.x)).x);
float finalY = Mathf.Clamp(currentTempPos.y, (currentNormalPos + currentChart.transform.InverseTransformVector(-Vector3.up * currentChartObj.cameraBorderOffsets.y)).y, (currentNormalPos + currentChart.transform.InverseTransformVector(Vector3.up * currentChartObj.cameraBorderOffsets.y)).y);
float finalZ = Mathf.Clamp(currentTempPos.z, (currentNormalPos + currentChart.transform.InverseTransformVector(-Vector3.right * currentChartObj.cameraBorderOffsets.x)).z, (currentNormalPos + currentChart.transform.InverseTransformVector(Vector3.right * currentChartObj.cameraBorderOffsets.x)).z);
Vector3 finalPosition = new Vector3(finalX,finalY,finalZ);
cameraTransform.transform.position = Vector3.Slerp(currentTempPos, finalPosition, 0.125f);
}
I am so confused about local and global and local world spaces...
Thanks in advance !
Answer by hsnzkg · Jun 04, 2021 at 10:58 AM
finally I fixed that
max vectors definations just ONCE
maxX = currentNormalPos + cameraTransform.transform.TransformVector(Vector3.right * currentChartObj.cameraBorderOffsets.x);
minX = currentNormalPos + cameraTransform.transform.TransformVector(-Vector3.right * currentChartObj.cameraBorderOffsets.x);
maxY = currentNormalPos + cameraTransform.transform.TransformDirection(Vector3.up * currentChartObj.cameraBorderOffsets.y);
minY = currentNormalPos + cameraTransform.transform.TransformDirection(-Vector3.up * currentChartObj.cameraBorderOffsets.y);
MOVEMENT CODE
private void MoveCamera(Vector3 force, float sens)
{
Vector3 desiredForce = new Vector3(force.x,force.y,0) * Time.deltaTime * sens;
cameraTransform.transform.Translate(desiredForce,Space.Self);
Vector3 currentTempPos = cameraTransform.transform.position;
float finalX = Mathf.Clamp(currentTempPos.x, Mathf.Min(minX.x, maxX.x), Mathf.Max(minX.x, maxX.x));
float finalY = Mathf.Clamp(currentTempPos.y, minY.y, maxY.y);
float finalZ = Mathf.Clamp(currentTempPos.z, Mathf.Min(minX.z, maxX.z), Mathf.Max(minX.z, maxX.z));
cameraTransform.transform.position = Vector3.Slerp(currentTempPos, new Vector3(finalX,finalY,finalZ), 0.125f);
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Throw 3D object parabolically -> From 2D Canvas to WorldSpace 1 Answer
Lean/Peak system 1 Answer