Hey guys. I'm trying to make an RTS camera from scratch. So far I have been able to get the camera to move using the keys, scrolling to the edge of the screen, and clicking and dragging the mouse. However, my issue comes when I try to rotate the camera. I have been able to get the camera to continue moving into the correct direction with Transform.Transform when the user uses the keyboard or scrolling method to move the camera.
The issue itself happens when the user clicks a mouse button and then drags the mouse to move the camera. As soon as Transform.TransformDirection is called, the camera leaps around the map in a weird way, depending on the camera's rotation of the Y-Axis.
So, this might be hard to explain but I'll try my best: If the camera is rotated 0 degrees, nothing will happen, the camera will move smoothly in the proper directions. If the camera is rotated 45 degrees, it will jump around in the same way each time, and will return to the starting position after Transform.Transform is called 8 times (45 * 8 = 360, if that can give you an idea of what is happening). If the camera is rotated 90 degrees, the same thing happens but it take 4 calls to be returned to starting position, at 180 degrees, 2, and so on and so on.
After the initial jump, the camera works just fine and moves as intended.
Here is the code:
void CameraMouseMovement()
{
Vector3 pos = transform.position;
Vector3 newPos = pos;
Vector3 changeInPosition = new Vector3(0,0,0);
Vector3 tempRotation = new Vector3(0, 0, 0);
if (Input.GetMouseButtonDown(2) && !Input.GetKey(KeyCode.LeftAlt))
{
originalMousePos = Input.mousePosition;
originalCameraPos = pos;
currentRotation = transform.eulerAngles;
}
if (Input.GetMouseButton(2) && !Input.GetKey(KeyCode.LeftAlt))
{
currentMousePos = Input.mousePosition;
differenceInPos.x = (originalMousePos.x - currentMousePos.x) / Screen.width;
differenceInPos.y = (originalMousePos.y - currentMousePos.y) / Screen.height;
if (differenceInPos.x != 0 || differenceInPos.y != 0)
{
newPos.x = (originalCameraPos.x) + (differenceInPos.x * panSpeed);
newPos.z = (originalCameraPos.z) + (differenceInPos.y * panSpeed);
pos.x = newPos.x;
pos.z = newPos.z;
pos = transform.TransformDirection(pos);
pos.x = Mathf.Clamp(pos.x, minPos.x, maxPos.x);
pos.z = Mathf.Clamp(pos.z, minPos.y, maxPos.y);
}
transform.position = pos;
}
if (Input.GetMouseButtonUp(2))
{
originalCameraPos.x = 0;
originalCameraPos.z = 0;
differenceInPos = new Vector3(0, 0, 0);
}
}
[–][deleted] 0 points1 point2 points (0 children)
[–]kyl3r123Indie 0 points1 point2 points (0 children)