- Home /
camera script, allow free movement of the camera up to a certain distance.
Hey peeps,
So I have been busy with tutorials for a camera script, it can be moved with WASD and also scroll to all 4 sides of the screen with the mouse. Furthermore you can rotate with ctrl+rightmouse.
However, I would like to set the camera up so that it is limited in how far a player can scroll around the map.
I would like to keep the functionality so far, however setting a limit as to how far the cam can move away from the player, and then a button to snap back to the player/ lock cam on the player.
Any ideas? or vids/tutorials/script examples
Answer by Tanoshimi2000 · Mar 05, 2014 at 06:26 PM
Seems to me like this is a simple problem. In the Update() just take the distance between the camera and the player, and if it's higher than a certain number, block the movement of the camera. Something like this...
void Update() {
bool canMove = true; // Default to letting them move
float limit = 100; // Play with this setting
// Get an approximate distance from camera and player
float delta = Mathf.Abs(camera.x - player.x) + Mathf.Abs(camera.z - player.z);
canMove = (delta<limit); // the camera is close enough, then we can move it.
// Camera moving code
if (canMove) {
// Insert you existing code for moving the camera here.
} //EndIf
Thank you very much for this. I shall try this out later on as its now 1:30 in the morning and I shall soon turn into a pumpkin! I will post back here and most probably tick this as answered.
Your answer
Follow this Question
Related Questions
Camera look position is always fixed.. need help. 3 Answers
Toggle Control between multiple turrets 0 Answers
Objectives based on object appear. 2 Answers
Code That Is Scene Dependent 1 Answer