SmoothDamp with position clamp
I have a camera following a player using 'SmoothDamp' however I wish to stop the camera going out of bounds of the background sprite.
I can do this separately with the 'SmoothDamp' but not with 'Mathf.Clamp'.
transform.position = Vector3.SmoothDamp(transform.position, targetPos, ref velocity, smoothTime);
or
bgBounds = GameObject.Find("Background").GetComponentInChildren<SpriteRenderer>();
bottomLeftLimit = bgBounds.sprite.bounds.min;
topRightLimit = bgBounds.sprite.bounds.max;
transform.position = new Vector3(Mathf.Clamp(transform.position.x, bottomLeftLimit.x, topRightLimit.x), Mathf.Clamp(transform.position.y, bottomLeftLimit.y, topRightLimit.y), transform.position.z);
c# unity3d
|
show 1 more comment
I have a camera following a player using 'SmoothDamp' however I wish to stop the camera going out of bounds of the background sprite.
I can do this separately with the 'SmoothDamp' but not with 'Mathf.Clamp'.
transform.position = Vector3.SmoothDamp(transform.position, targetPos, ref velocity, smoothTime);
or
bgBounds = GameObject.Find("Background").GetComponentInChildren<SpriteRenderer>();
bottomLeftLimit = bgBounds.sprite.bounds.min;
topRightLimit = bgBounds.sprite.bounds.max;
transform.position = new Vector3(Mathf.Clamp(transform.position.x, bottomLeftLimit.x, topRightLimit.x), Mathf.Clamp(transform.position.y, bottomLeftLimit.y, topRightLimit.y), transform.position.z);
c# unity3d
Does it successfully keep the center point of the camera's view within the background's bounds? Also, are you using an orthographic camera?
– Ruzihm
Nov 27 '18 at 17:57
The first snippet of code is what I am using and the camera is going out of bounds. I am not using the second snippet but it is what I am trying to achieve.
– Keith Power
Nov 27 '18 at 18:03
Are you using an orthographic camera?
– Ruzihm
Nov 27 '18 at 18:04
1
Sorry yes, and it is a 2d top down game
– Keith Power
Nov 27 '18 at 18:05
1
Yes, I havebgBounds = GameObject.Find
in theStart
– Keith Power
Nov 27 '18 at 19:33
|
show 1 more comment
I have a camera following a player using 'SmoothDamp' however I wish to stop the camera going out of bounds of the background sprite.
I can do this separately with the 'SmoothDamp' but not with 'Mathf.Clamp'.
transform.position = Vector3.SmoothDamp(transform.position, targetPos, ref velocity, smoothTime);
or
bgBounds = GameObject.Find("Background").GetComponentInChildren<SpriteRenderer>();
bottomLeftLimit = bgBounds.sprite.bounds.min;
topRightLimit = bgBounds.sprite.bounds.max;
transform.position = new Vector3(Mathf.Clamp(transform.position.x, bottomLeftLimit.x, topRightLimit.x), Mathf.Clamp(transform.position.y, bottomLeftLimit.y, topRightLimit.y), transform.position.z);
c# unity3d
I have a camera following a player using 'SmoothDamp' however I wish to stop the camera going out of bounds of the background sprite.
I can do this separately with the 'SmoothDamp' but not with 'Mathf.Clamp'.
transform.position = Vector3.SmoothDamp(transform.position, targetPos, ref velocity, smoothTime);
or
bgBounds = GameObject.Find("Background").GetComponentInChildren<SpriteRenderer>();
bottomLeftLimit = bgBounds.sprite.bounds.min;
topRightLimit = bgBounds.sprite.bounds.max;
transform.position = new Vector3(Mathf.Clamp(transform.position.x, bottomLeftLimit.x, topRightLimit.x), Mathf.Clamp(transform.position.y, bottomLeftLimit.y, topRightLimit.y), transform.position.z);
c# unity3d
c# unity3d
edited Nov 27 '18 at 18:17
Programmer
77.7k1089158
77.7k1089158
asked Nov 27 '18 at 17:50
Keith PowerKeith Power
5,181164896
5,181164896
Does it successfully keep the center point of the camera's view within the background's bounds? Also, are you using an orthographic camera?
– Ruzihm
Nov 27 '18 at 17:57
The first snippet of code is what I am using and the camera is going out of bounds. I am not using the second snippet but it is what I am trying to achieve.
– Keith Power
Nov 27 '18 at 18:03
Are you using an orthographic camera?
– Ruzihm
Nov 27 '18 at 18:04
1
Sorry yes, and it is a 2d top down game
– Keith Power
Nov 27 '18 at 18:05
1
Yes, I havebgBounds = GameObject.Find
in theStart
– Keith Power
Nov 27 '18 at 19:33
|
show 1 more comment
Does it successfully keep the center point of the camera's view within the background's bounds? Also, are you using an orthographic camera?
– Ruzihm
Nov 27 '18 at 17:57
The first snippet of code is what I am using and the camera is going out of bounds. I am not using the second snippet but it is what I am trying to achieve.
– Keith Power
Nov 27 '18 at 18:03
Are you using an orthographic camera?
– Ruzihm
Nov 27 '18 at 18:04
1
Sorry yes, and it is a 2d top down game
– Keith Power
Nov 27 '18 at 18:05
1
Yes, I havebgBounds = GameObject.Find
in theStart
– Keith Power
Nov 27 '18 at 19:33
Does it successfully keep the center point of the camera's view within the background's bounds? Also, are you using an orthographic camera?
– Ruzihm
Nov 27 '18 at 17:57
Does it successfully keep the center point of the camera's view within the background's bounds? Also, are you using an orthographic camera?
– Ruzihm
Nov 27 '18 at 17:57
The first snippet of code is what I am using and the camera is going out of bounds. I am not using the second snippet but it is what I am trying to achieve.
– Keith Power
Nov 27 '18 at 18:03
The first snippet of code is what I am using and the camera is going out of bounds. I am not using the second snippet but it is what I am trying to achieve.
– Keith Power
Nov 27 '18 at 18:03
Are you using an orthographic camera?
– Ruzihm
Nov 27 '18 at 18:04
Are you using an orthographic camera?
– Ruzihm
Nov 27 '18 at 18:04
1
1
Sorry yes, and it is a 2d top down game
– Keith Power
Nov 27 '18 at 18:05
Sorry yes, and it is a 2d top down game
– Keith Power
Nov 27 '18 at 18:05
1
1
Yes, I have
bgBounds = GameObject.Find
in the Start
– Keith Power
Nov 27 '18 at 19:33
Yes, I have
bgBounds = GameObject.Find
in the Start
– Keith Power
Nov 27 '18 at 19:33
|
show 1 more comment
1 Answer
1
active
oldest
votes
In order to ensure that the boundaries of an orthographic camera don't leave a Bounds
you need to include the extents of the camera's view as padding to your clamps.
You can use camera.orthographicSize
and camera.aspect
to get the extents of the camera's view.
// Clamp the target position to be within the boundaries
float cameraVertExtent = camera.orthographicSize;
float cameraHorizExtent = camera.orthographicSize * camera.aspect;
Vector3 clampedTargetPos = new Vector3(
Mathf.Clamp(targetPos.x, bottomLeftLimit.x+cameraHorizExtent, topRightLimit.x-cameraHorizExtent),
Mathf.Clamp(targetPos.y, bottomLeftLimit.y+cameraVertExtent, topRightLimit.y-cameraVertExtent),
transform.position.z
);
// SmoothDamp to the clamped target position.
transform.position = Vector3.SmoothDamp(transform.position, clampedTargetPos, ref velocity, smoothTime);
If you weren't using an orthographic camera, you would need to find where the edges of the camera's view intersect with the background, and solve for the necessary padding from there.
thank you, this makes sense. However when I go to the top of the screen for example the camera moves to the bottom of the screen rather then just stopping. When I move player down again the camera moves up to meet the player
– Keith Power
Nov 27 '18 at 18:24
I did have to change thecamera.orthographicSize
togetComponent<Camera>(). orthographicSize
– Keith Power
Nov 27 '18 at 18:27
1
If you're calling this code on every frame, you should add aprivate Camera camera
property to the class and putcamera = getComponent<Camera>();
inStart
so that you don't have to callGetComponent
on every frame. it is an expensive operation
– Ruzihm
Nov 27 '18 at 18:33
add a comment |
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',
autoActivateHeartbeat: false,
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%2f53505383%2fsmoothdamp-with-position-clamp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
In order to ensure that the boundaries of an orthographic camera don't leave a Bounds
you need to include the extents of the camera's view as padding to your clamps.
You can use camera.orthographicSize
and camera.aspect
to get the extents of the camera's view.
// Clamp the target position to be within the boundaries
float cameraVertExtent = camera.orthographicSize;
float cameraHorizExtent = camera.orthographicSize * camera.aspect;
Vector3 clampedTargetPos = new Vector3(
Mathf.Clamp(targetPos.x, bottomLeftLimit.x+cameraHorizExtent, topRightLimit.x-cameraHorizExtent),
Mathf.Clamp(targetPos.y, bottomLeftLimit.y+cameraVertExtent, topRightLimit.y-cameraVertExtent),
transform.position.z
);
// SmoothDamp to the clamped target position.
transform.position = Vector3.SmoothDamp(transform.position, clampedTargetPos, ref velocity, smoothTime);
If you weren't using an orthographic camera, you would need to find where the edges of the camera's view intersect with the background, and solve for the necessary padding from there.
thank you, this makes sense. However when I go to the top of the screen for example the camera moves to the bottom of the screen rather then just stopping. When I move player down again the camera moves up to meet the player
– Keith Power
Nov 27 '18 at 18:24
I did have to change thecamera.orthographicSize
togetComponent<Camera>(). orthographicSize
– Keith Power
Nov 27 '18 at 18:27
1
If you're calling this code on every frame, you should add aprivate Camera camera
property to the class and putcamera = getComponent<Camera>();
inStart
so that you don't have to callGetComponent
on every frame. it is an expensive operation
– Ruzihm
Nov 27 '18 at 18:33
add a comment |
In order to ensure that the boundaries of an orthographic camera don't leave a Bounds
you need to include the extents of the camera's view as padding to your clamps.
You can use camera.orthographicSize
and camera.aspect
to get the extents of the camera's view.
// Clamp the target position to be within the boundaries
float cameraVertExtent = camera.orthographicSize;
float cameraHorizExtent = camera.orthographicSize * camera.aspect;
Vector3 clampedTargetPos = new Vector3(
Mathf.Clamp(targetPos.x, bottomLeftLimit.x+cameraHorizExtent, topRightLimit.x-cameraHorizExtent),
Mathf.Clamp(targetPos.y, bottomLeftLimit.y+cameraVertExtent, topRightLimit.y-cameraVertExtent),
transform.position.z
);
// SmoothDamp to the clamped target position.
transform.position = Vector3.SmoothDamp(transform.position, clampedTargetPos, ref velocity, smoothTime);
If you weren't using an orthographic camera, you would need to find where the edges of the camera's view intersect with the background, and solve for the necessary padding from there.
thank you, this makes sense. However when I go to the top of the screen for example the camera moves to the bottom of the screen rather then just stopping. When I move player down again the camera moves up to meet the player
– Keith Power
Nov 27 '18 at 18:24
I did have to change thecamera.orthographicSize
togetComponent<Camera>(). orthographicSize
– Keith Power
Nov 27 '18 at 18:27
1
If you're calling this code on every frame, you should add aprivate Camera camera
property to the class and putcamera = getComponent<Camera>();
inStart
so that you don't have to callGetComponent
on every frame. it is an expensive operation
– Ruzihm
Nov 27 '18 at 18:33
add a comment |
In order to ensure that the boundaries of an orthographic camera don't leave a Bounds
you need to include the extents of the camera's view as padding to your clamps.
You can use camera.orthographicSize
and camera.aspect
to get the extents of the camera's view.
// Clamp the target position to be within the boundaries
float cameraVertExtent = camera.orthographicSize;
float cameraHorizExtent = camera.orthographicSize * camera.aspect;
Vector3 clampedTargetPos = new Vector3(
Mathf.Clamp(targetPos.x, bottomLeftLimit.x+cameraHorizExtent, topRightLimit.x-cameraHorizExtent),
Mathf.Clamp(targetPos.y, bottomLeftLimit.y+cameraVertExtent, topRightLimit.y-cameraVertExtent),
transform.position.z
);
// SmoothDamp to the clamped target position.
transform.position = Vector3.SmoothDamp(transform.position, clampedTargetPos, ref velocity, smoothTime);
If you weren't using an orthographic camera, you would need to find where the edges of the camera's view intersect with the background, and solve for the necessary padding from there.
In order to ensure that the boundaries of an orthographic camera don't leave a Bounds
you need to include the extents of the camera's view as padding to your clamps.
You can use camera.orthographicSize
and camera.aspect
to get the extents of the camera's view.
// Clamp the target position to be within the boundaries
float cameraVertExtent = camera.orthographicSize;
float cameraHorizExtent = camera.orthographicSize * camera.aspect;
Vector3 clampedTargetPos = new Vector3(
Mathf.Clamp(targetPos.x, bottomLeftLimit.x+cameraHorizExtent, topRightLimit.x-cameraHorizExtent),
Mathf.Clamp(targetPos.y, bottomLeftLimit.y+cameraVertExtent, topRightLimit.y-cameraVertExtent),
transform.position.z
);
// SmoothDamp to the clamped target position.
transform.position = Vector3.SmoothDamp(transform.position, clampedTargetPos, ref velocity, smoothTime);
If you weren't using an orthographic camera, you would need to find where the edges of the camera's view intersect with the background, and solve for the necessary padding from there.
edited Nov 27 '18 at 21:20
answered Nov 27 '18 at 18:07
RuzihmRuzihm
3,71611627
3,71611627
thank you, this makes sense. However when I go to the top of the screen for example the camera moves to the bottom of the screen rather then just stopping. When I move player down again the camera moves up to meet the player
– Keith Power
Nov 27 '18 at 18:24
I did have to change thecamera.orthographicSize
togetComponent<Camera>(). orthographicSize
– Keith Power
Nov 27 '18 at 18:27
1
If you're calling this code on every frame, you should add aprivate Camera camera
property to the class and putcamera = getComponent<Camera>();
inStart
so that you don't have to callGetComponent
on every frame. it is an expensive operation
– Ruzihm
Nov 27 '18 at 18:33
add a comment |
thank you, this makes sense. However when I go to the top of the screen for example the camera moves to the bottom of the screen rather then just stopping. When I move player down again the camera moves up to meet the player
– Keith Power
Nov 27 '18 at 18:24
I did have to change thecamera.orthographicSize
togetComponent<Camera>(). orthographicSize
– Keith Power
Nov 27 '18 at 18:27
1
If you're calling this code on every frame, you should add aprivate Camera camera
property to the class and putcamera = getComponent<Camera>();
inStart
so that you don't have to callGetComponent
on every frame. it is an expensive operation
– Ruzihm
Nov 27 '18 at 18:33
thank you, this makes sense. However when I go to the top of the screen for example the camera moves to the bottom of the screen rather then just stopping. When I move player down again the camera moves up to meet the player
– Keith Power
Nov 27 '18 at 18:24
thank you, this makes sense. However when I go to the top of the screen for example the camera moves to the bottom of the screen rather then just stopping. When I move player down again the camera moves up to meet the player
– Keith Power
Nov 27 '18 at 18:24
I did have to change the
camera.orthographicSize
to getComponent<Camera>(). orthographicSize
– Keith Power
Nov 27 '18 at 18:27
I did have to change the
camera.orthographicSize
to getComponent<Camera>(). orthographicSize
– Keith Power
Nov 27 '18 at 18:27
1
1
If you're calling this code on every frame, you should add a
private Camera camera
property to the class and put camera = getComponent<Camera>();
in Start
so that you don't have to call GetComponent
on every frame. it is an expensive operation– Ruzihm
Nov 27 '18 at 18:33
If you're calling this code on every frame, you should add a
private Camera camera
property to the class and put camera = getComponent<Camera>();
in Start
so that you don't have to call GetComponent
on every frame. it is an expensive operation– Ruzihm
Nov 27 '18 at 18:33
add a comment |
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.
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%2f53505383%2fsmoothdamp-with-position-clamp%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
Does it successfully keep the center point of the camera's view within the background's bounds? Also, are you using an orthographic camera?
– Ruzihm
Nov 27 '18 at 17:57
The first snippet of code is what I am using and the camera is going out of bounds. I am not using the second snippet but it is what I am trying to achieve.
– Keith Power
Nov 27 '18 at 18:03
Are you using an orthographic camera?
– Ruzihm
Nov 27 '18 at 18:04
1
Sorry yes, and it is a 2d top down game
– Keith Power
Nov 27 '18 at 18:05
1
Yes, I have
bgBounds = GameObject.Find
in theStart
– Keith Power
Nov 27 '18 at 19:33