- Home /
Help with rotating Camera around Pivotpoint(from A to B)
I'm trying to create a leaning system based off of a PivotPoint(Vector3) instead of placing the camera as the child of the PivotPoint.
So when you hit 'Q' it rotates the camera around the PivotPoint to 70 degrees over a speed constant, and when you hit 'E' it rotates the the camera around the PivotPoint to 110 degrees over a speed constant as well. Does anyone know how to do this?
After 6 hours of headbashing, this is one of my attempts at trying what to do, that I discussed above.
public Transform camera;
public Transform pivotPoint;
bool leanLeft = false;
bool leanRight = false;
public void Update(){
if (Input.GetKeyDown(KeyCode.Q))
{
leanRight = false;
leanLeft = !leanLeft;
}
if (Input.GetKeyDown(KeyCode.E))
{
leanLeft = false;
leanRight = !leanRight;
}
if (leanLeft)
{
curAngle = Mathf.MoveTowardsAngle(curAngle, maxAngle, speed * Time.deltaTime);
}
else if (leanRight)
{
curAngle = Mathf.MoveTowardsAngle(curAngle, -maxAngle, speed * Time.deltaTime);
}
else
{
curAngle = Mathf.MoveTowardsAngle(curAngle, 0f, speed * Time.deltaTime);
}
//camera.transform.localRotation = Quaternion.AngleAxis(curAngle, Vector3.forward);
camera.transform.RotateAround(pivotPoint.position, Vector3.forward, curAngle);
}
Answer by michi_b · Oct 29, 2018 at 06:38 AM
The only thing in your code that is weird to me at first glance is the rotation axis parameter of RotateAround(). Since you use the fields "leanLeft" and "leandRight", I'd expect that you want to rotate the camera around the pivot on the horizontal (xz) plane. Then you should use Vector.up (y axis) as your rotation axis.
Vector3.forward is used becuase I would like to rotate on the Z axis, using Vector3.up makes it go side to side ins$$anonymous$$d of left to right. Which leads me back to RotateAround, it does not seem to rotate around the given point that has been assigned, so would you know how to do this?
Okay, so something close to a barrel roll? In which way does your code not behave like you want it? $$anonymous$$aybe you should take camera.transform.forward or pivot.transform.forward as your rotation axis ins$$anonymous$$d of Vector3.forward (in case your camera does not always point along the z axis)
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Trouble with Camera Rotation Math 2 Answers
How do I stop my camera flying? 0 Answers
2D Raycast in any direction 1 Answer
2D Aiming in a 3D game 0 Answers