clamping camera movement to map size and camera corners
up vote
0
down vote
favorite
I'm working on RTS camera movement,
and i'm trying to clamp its movement
base on the map size and the corners of the camera.
this is my code:
public void Init(Vector2 mapSize)
{
_camera = GetComponent<Camera>();
_movePosition = transform.position =
new Vector3(CameraStartPosition.x, CameraHeight, CameraStartPosition.z);
var frustumHeight = 2.0f * CameraHeight * Mathf.Tan(_camera.fieldOfView * 0.5f * Mathf.Deg2Rad);
var frustumWidth = frustumHeight * _camera.aspect;
var floorZSize = mapSize.y * _mapScaleFactor;
var angle = Mathf.Sin(transform.localRotation.eulerAngles.x * Mathf.Deg2Rad);
var distance = frustumHeight * 0.5f / Mathf.Tan(_camera.fieldOfView * 0.5f * Mathf.Deg2Rad);
var hypotenuse = distance / angle;
var adjacent = Mathf.Sqrt(Mathf.Pow(hypotenuse,2) - Mathf.Pow(distance,2));
var zMax = floorZSize - adjacent ;
var zMin = floorZSize + adjacent ;
Debug.Log(
$"frustumWidth: {frustumWidth} frustumHeight: {frustumHeight} " +
$"filed of view: {_camera.fieldOfView} distance: {distance} " +
$"mapHeight {floorZSize} angle{angle} hypotenuse: {hypotenuse} adjacent: {adjacent} zMax: {zMax} zMin: {zMin}");
BoundsMin = new Vector3(
_mapScaleFactor*(-mapSize.x) + CameraHeight*Mathf.Sin(transform.localRotation.eulerAngles.z*Mathf.Deg2Rad),
CameraHeight,
-zMin);
BoundsMax = new Vector3(
_mapScaleFactor*mapSize.x + CameraHeight*Mathf.Sin(transform.localRotation.eulerAngles.z*Mathf.Deg2Rad),
CameraHeight,
zMax );
}
private void Update()
{
if (_camera == null) return;
if (Input.GetKey(_moveUp.Key))
{
_movePosition.z += MovementSpeed * Time.deltaTime;
}
if (Input.GetKey(_moveRight.Key))
{
_movePosition.x += MovementSpeed * Time.deltaTime;
}
if (Input.GetKey(_moveDown.Key))
{
_movePosition.z -= MovementSpeed * Time.deltaTime;
}
if (Input.GetKey(_movLeft.Key))
{
_movePosition.x -= MovementSpeed * Time.deltaTime;
}
var cameraMoveDirection = (_movePosition - transform.position).normalized;
var distance = Vector3.Distance(_movePosition,transform.position);
_movePosition = new Vector3(
Mathf.Clamp(_movePosition.x, BoundsMin.x, BoundsMax.x),
Mathf.Clamp(_movePosition.y, BoundsMin.y, BoundsMax.y),
Mathf.Clamp(_movePosition.z, BoundsMin.z, BoundsMax.z));
if (distance > 0)
{
var newCameraPosition = transform.position + (cameraMoveDirection * distance * MovementSpeed * Time.deltaTime);
newCameraPosition = new Vector3(
Mathf.Clamp(newCameraPosition.x, BoundsMin.x, BoundsMax.x),
Mathf.Clamp(newCameraPosition.y, BoundsMin.y, BoundsMax.y),
Mathf.Clamp(newCameraPosition.z, BoundsMin.z, BoundsMax.z));
var distanceAfterMoving = Vector3.Distance(newCameraPosition, _movePosition);
if (distanceAfterMoving > distance)
{
newCameraPosition = _movePosition;
}
transform.position = newCameraPosition;
}
}
so I know how the get all the values like the angle, distance, hypotenuse and adjacent of the camera triangle, but I know I'm still missing something in the trigonometry of the camera.
My current X rotation of the camera is 40.
Just to make myself clear I want that camera movement will clamp when camera position will be for example MapXMin - cameraLeftCorner.
Can someone please help me figuring out what am I doing wrong ?
c# unity3d
add a comment |
up vote
0
down vote
favorite
I'm working on RTS camera movement,
and i'm trying to clamp its movement
base on the map size and the corners of the camera.
this is my code:
public void Init(Vector2 mapSize)
{
_camera = GetComponent<Camera>();
_movePosition = transform.position =
new Vector3(CameraStartPosition.x, CameraHeight, CameraStartPosition.z);
var frustumHeight = 2.0f * CameraHeight * Mathf.Tan(_camera.fieldOfView * 0.5f * Mathf.Deg2Rad);
var frustumWidth = frustumHeight * _camera.aspect;
var floorZSize = mapSize.y * _mapScaleFactor;
var angle = Mathf.Sin(transform.localRotation.eulerAngles.x * Mathf.Deg2Rad);
var distance = frustumHeight * 0.5f / Mathf.Tan(_camera.fieldOfView * 0.5f * Mathf.Deg2Rad);
var hypotenuse = distance / angle;
var adjacent = Mathf.Sqrt(Mathf.Pow(hypotenuse,2) - Mathf.Pow(distance,2));
var zMax = floorZSize - adjacent ;
var zMin = floorZSize + adjacent ;
Debug.Log(
$"frustumWidth: {frustumWidth} frustumHeight: {frustumHeight} " +
$"filed of view: {_camera.fieldOfView} distance: {distance} " +
$"mapHeight {floorZSize} angle{angle} hypotenuse: {hypotenuse} adjacent: {adjacent} zMax: {zMax} zMin: {zMin}");
BoundsMin = new Vector3(
_mapScaleFactor*(-mapSize.x) + CameraHeight*Mathf.Sin(transform.localRotation.eulerAngles.z*Mathf.Deg2Rad),
CameraHeight,
-zMin);
BoundsMax = new Vector3(
_mapScaleFactor*mapSize.x + CameraHeight*Mathf.Sin(transform.localRotation.eulerAngles.z*Mathf.Deg2Rad),
CameraHeight,
zMax );
}
private void Update()
{
if (_camera == null) return;
if (Input.GetKey(_moveUp.Key))
{
_movePosition.z += MovementSpeed * Time.deltaTime;
}
if (Input.GetKey(_moveRight.Key))
{
_movePosition.x += MovementSpeed * Time.deltaTime;
}
if (Input.GetKey(_moveDown.Key))
{
_movePosition.z -= MovementSpeed * Time.deltaTime;
}
if (Input.GetKey(_movLeft.Key))
{
_movePosition.x -= MovementSpeed * Time.deltaTime;
}
var cameraMoveDirection = (_movePosition - transform.position).normalized;
var distance = Vector3.Distance(_movePosition,transform.position);
_movePosition = new Vector3(
Mathf.Clamp(_movePosition.x, BoundsMin.x, BoundsMax.x),
Mathf.Clamp(_movePosition.y, BoundsMin.y, BoundsMax.y),
Mathf.Clamp(_movePosition.z, BoundsMin.z, BoundsMax.z));
if (distance > 0)
{
var newCameraPosition = transform.position + (cameraMoveDirection * distance * MovementSpeed * Time.deltaTime);
newCameraPosition = new Vector3(
Mathf.Clamp(newCameraPosition.x, BoundsMin.x, BoundsMax.x),
Mathf.Clamp(newCameraPosition.y, BoundsMin.y, BoundsMax.y),
Mathf.Clamp(newCameraPosition.z, BoundsMin.z, BoundsMax.z));
var distanceAfterMoving = Vector3.Distance(newCameraPosition, _movePosition);
if (distanceAfterMoving > distance)
{
newCameraPosition = _movePosition;
}
transform.position = newCameraPosition;
}
}
so I know how the get all the values like the angle, distance, hypotenuse and adjacent of the camera triangle, but I know I'm still missing something in the trigonometry of the camera.
My current X rotation of the camera is 40.
Just to make myself clear I want that camera movement will clamp when camera position will be for example MapXMin - cameraLeftCorner.
Can someone please help me figuring out what am I doing wrong ?
c# unity3d
You should check Cinemachine (unity3d.com/es/learn/tutorials/topics/animation/…)
– Lotan
Nov 20 at 12:19
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm working on RTS camera movement,
and i'm trying to clamp its movement
base on the map size and the corners of the camera.
this is my code:
public void Init(Vector2 mapSize)
{
_camera = GetComponent<Camera>();
_movePosition = transform.position =
new Vector3(CameraStartPosition.x, CameraHeight, CameraStartPosition.z);
var frustumHeight = 2.0f * CameraHeight * Mathf.Tan(_camera.fieldOfView * 0.5f * Mathf.Deg2Rad);
var frustumWidth = frustumHeight * _camera.aspect;
var floorZSize = mapSize.y * _mapScaleFactor;
var angle = Mathf.Sin(transform.localRotation.eulerAngles.x * Mathf.Deg2Rad);
var distance = frustumHeight * 0.5f / Mathf.Tan(_camera.fieldOfView * 0.5f * Mathf.Deg2Rad);
var hypotenuse = distance / angle;
var adjacent = Mathf.Sqrt(Mathf.Pow(hypotenuse,2) - Mathf.Pow(distance,2));
var zMax = floorZSize - adjacent ;
var zMin = floorZSize + adjacent ;
Debug.Log(
$"frustumWidth: {frustumWidth} frustumHeight: {frustumHeight} " +
$"filed of view: {_camera.fieldOfView} distance: {distance} " +
$"mapHeight {floorZSize} angle{angle} hypotenuse: {hypotenuse} adjacent: {adjacent} zMax: {zMax} zMin: {zMin}");
BoundsMin = new Vector3(
_mapScaleFactor*(-mapSize.x) + CameraHeight*Mathf.Sin(transform.localRotation.eulerAngles.z*Mathf.Deg2Rad),
CameraHeight,
-zMin);
BoundsMax = new Vector3(
_mapScaleFactor*mapSize.x + CameraHeight*Mathf.Sin(transform.localRotation.eulerAngles.z*Mathf.Deg2Rad),
CameraHeight,
zMax );
}
private void Update()
{
if (_camera == null) return;
if (Input.GetKey(_moveUp.Key))
{
_movePosition.z += MovementSpeed * Time.deltaTime;
}
if (Input.GetKey(_moveRight.Key))
{
_movePosition.x += MovementSpeed * Time.deltaTime;
}
if (Input.GetKey(_moveDown.Key))
{
_movePosition.z -= MovementSpeed * Time.deltaTime;
}
if (Input.GetKey(_movLeft.Key))
{
_movePosition.x -= MovementSpeed * Time.deltaTime;
}
var cameraMoveDirection = (_movePosition - transform.position).normalized;
var distance = Vector3.Distance(_movePosition,transform.position);
_movePosition = new Vector3(
Mathf.Clamp(_movePosition.x, BoundsMin.x, BoundsMax.x),
Mathf.Clamp(_movePosition.y, BoundsMin.y, BoundsMax.y),
Mathf.Clamp(_movePosition.z, BoundsMin.z, BoundsMax.z));
if (distance > 0)
{
var newCameraPosition = transform.position + (cameraMoveDirection * distance * MovementSpeed * Time.deltaTime);
newCameraPosition = new Vector3(
Mathf.Clamp(newCameraPosition.x, BoundsMin.x, BoundsMax.x),
Mathf.Clamp(newCameraPosition.y, BoundsMin.y, BoundsMax.y),
Mathf.Clamp(newCameraPosition.z, BoundsMin.z, BoundsMax.z));
var distanceAfterMoving = Vector3.Distance(newCameraPosition, _movePosition);
if (distanceAfterMoving > distance)
{
newCameraPosition = _movePosition;
}
transform.position = newCameraPosition;
}
}
so I know how the get all the values like the angle, distance, hypotenuse and adjacent of the camera triangle, but I know I'm still missing something in the trigonometry of the camera.
My current X rotation of the camera is 40.
Just to make myself clear I want that camera movement will clamp when camera position will be for example MapXMin - cameraLeftCorner.
Can someone please help me figuring out what am I doing wrong ?
c# unity3d
I'm working on RTS camera movement,
and i'm trying to clamp its movement
base on the map size and the corners of the camera.
this is my code:
public void Init(Vector2 mapSize)
{
_camera = GetComponent<Camera>();
_movePosition = transform.position =
new Vector3(CameraStartPosition.x, CameraHeight, CameraStartPosition.z);
var frustumHeight = 2.0f * CameraHeight * Mathf.Tan(_camera.fieldOfView * 0.5f * Mathf.Deg2Rad);
var frustumWidth = frustumHeight * _camera.aspect;
var floorZSize = mapSize.y * _mapScaleFactor;
var angle = Mathf.Sin(transform.localRotation.eulerAngles.x * Mathf.Deg2Rad);
var distance = frustumHeight * 0.5f / Mathf.Tan(_camera.fieldOfView * 0.5f * Mathf.Deg2Rad);
var hypotenuse = distance / angle;
var adjacent = Mathf.Sqrt(Mathf.Pow(hypotenuse,2) - Mathf.Pow(distance,2));
var zMax = floorZSize - adjacent ;
var zMin = floorZSize + adjacent ;
Debug.Log(
$"frustumWidth: {frustumWidth} frustumHeight: {frustumHeight} " +
$"filed of view: {_camera.fieldOfView} distance: {distance} " +
$"mapHeight {floorZSize} angle{angle} hypotenuse: {hypotenuse} adjacent: {adjacent} zMax: {zMax} zMin: {zMin}");
BoundsMin = new Vector3(
_mapScaleFactor*(-mapSize.x) + CameraHeight*Mathf.Sin(transform.localRotation.eulerAngles.z*Mathf.Deg2Rad),
CameraHeight,
-zMin);
BoundsMax = new Vector3(
_mapScaleFactor*mapSize.x + CameraHeight*Mathf.Sin(transform.localRotation.eulerAngles.z*Mathf.Deg2Rad),
CameraHeight,
zMax );
}
private void Update()
{
if (_camera == null) return;
if (Input.GetKey(_moveUp.Key))
{
_movePosition.z += MovementSpeed * Time.deltaTime;
}
if (Input.GetKey(_moveRight.Key))
{
_movePosition.x += MovementSpeed * Time.deltaTime;
}
if (Input.GetKey(_moveDown.Key))
{
_movePosition.z -= MovementSpeed * Time.deltaTime;
}
if (Input.GetKey(_movLeft.Key))
{
_movePosition.x -= MovementSpeed * Time.deltaTime;
}
var cameraMoveDirection = (_movePosition - transform.position).normalized;
var distance = Vector3.Distance(_movePosition,transform.position);
_movePosition = new Vector3(
Mathf.Clamp(_movePosition.x, BoundsMin.x, BoundsMax.x),
Mathf.Clamp(_movePosition.y, BoundsMin.y, BoundsMax.y),
Mathf.Clamp(_movePosition.z, BoundsMin.z, BoundsMax.z));
if (distance > 0)
{
var newCameraPosition = transform.position + (cameraMoveDirection * distance * MovementSpeed * Time.deltaTime);
newCameraPosition = new Vector3(
Mathf.Clamp(newCameraPosition.x, BoundsMin.x, BoundsMax.x),
Mathf.Clamp(newCameraPosition.y, BoundsMin.y, BoundsMax.y),
Mathf.Clamp(newCameraPosition.z, BoundsMin.z, BoundsMax.z));
var distanceAfterMoving = Vector3.Distance(newCameraPosition, _movePosition);
if (distanceAfterMoving > distance)
{
newCameraPosition = _movePosition;
}
transform.position = newCameraPosition;
}
}
so I know how the get all the values like the angle, distance, hypotenuse and adjacent of the camera triangle, but I know I'm still missing something in the trigonometry of the camera.
My current X rotation of the camera is 40.
Just to make myself clear I want that camera movement will clamp when camera position will be for example MapXMin - cameraLeftCorner.
Can someone please help me figuring out what am I doing wrong ?
c# unity3d
c# unity3d
edited Nov 20 at 10:56
asked Nov 20 at 10:42
SilverCrow
4416
4416
You should check Cinemachine (unity3d.com/es/learn/tutorials/topics/animation/…)
– Lotan
Nov 20 at 12:19
add a comment |
You should check Cinemachine (unity3d.com/es/learn/tutorials/topics/animation/…)
– Lotan
Nov 20 at 12:19
You should check Cinemachine (unity3d.com/es/learn/tutorials/topics/animation/…)
– Lotan
Nov 20 at 12:19
You should check Cinemachine (unity3d.com/es/learn/tutorials/topics/animation/…)
– Lotan
Nov 20 at 12:19
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391226%2fclamping-camera-movement-to-map-size-and-camera-corners%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391226%2fclamping-camera-movement-to-map-size-and-camera-corners%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You should check Cinemachine (unity3d.com/es/learn/tutorials/topics/animation/…)
– Lotan
Nov 20 at 12:19