Orbit camera with keyboard

I am having a surprisingly hard time finding scripts/tutorials online allowing my camera to orbit my player using only the keyboard keys. There are tons for mouse orbit, but surprisingly few for keyboard. Anyway, I want to be able to use wasd or arrow keys to make the camera spin around my player, but always looking at the player. The always looking part I already have, but the rotating is giving me trouble.

One of the major complications is that I am adding this functionality onto another script in an asset I purchased, so some parts are pretty set in stone (its a multiplayer thing so I don’t want to go altering too much)

I tried this code I found:

``````if (target)
{
x -= Input.GetAxis("Horizontal") * xSpeed * 0.02f;
y += Input.GetAxis("Vertical") * ySpeed * 0.02f;

y = ClampAngle(y, yMinLimit, yMaxLimit);

distanceCam -= Input.GetAxis("Fire1") * zoomSpd * 0.02f;
distanceCam += Input.GetAxis("Fire2") * zoomSpd * 0.02f;

Quaternion rotation = Quaternion.Euler(y, x, 0.0f);

Vector3 position = rotation * new Vector3(0.0f, 0.0f, 0.0f) + target.position;

transform.rotation = rotation;
transform.position = position;
}

public static float ClampAngle(float angle, float min, float max)
{
if (angle < -360.0f)
{
angle += 360.0f;
}
if (angle > 360.0f)
{
angle -= 360.0f;
}
return Mathf.Clamp(angle, min, max);
}
``````

This kind of works, except it sets the camera to the origin point of my player. It will look around, but it emulates mouse look, not camera rotation. Clearly I am looking in the wrong place so any help from anyone would be greatly appreciated!

Here is the premade code that I am trying to work around:

``````protected void UpdateCamera()
{
Camera camera = Camera.main;

// Calculate the current rotation angles
float wantedHeight = target.position.y + height;
float currentHeight = camera.transform.position.y;

// Damp the rotation around the y-axis
// Damp the height
//currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
currentHeight = wantedHeight;

// Convert the angle into a rotation
// Set the position of the camera on the x-z plane to:
// distance meters behind the target
camera.transform.position = target.position;
camera.transform.position -= cameraRotation * distance;

// Set the height of the camera
camera.transform.position = new Vector3(camera.transform.position.x, currentHeight, camera.transform.position.z);

// Always look at the target
camera.transform.LookAt(target);
}
``````

I tried to add in a simple RotateAround at the end after camera.transform.LookAt, something like thiss:

``````if (Input.GetAxis("Horizontal") > 0.0f)
{
transform.RotateAround(target.position, Vector3.up, 30 * Time.deltaTime);
}
``````

which just barely works. Now when I press the buttons the camera moves like half a degree for a moment, then stops. I can move it back just slightly. I think that batch of code is fighting with another batch of code, but I’m not sure which ones.

I think I’m making progress. I moved this part:

``````[LIST=1]
[*]camera.transform.position = target.position;
[*]            camera.transform.position -= cameraRotation * distance;
``````

[/LIST]
to the Start() function and now the camera rotates! Unfortunately, it only rotates in one direction, and still moves a little after I stop pressing. So still a bit to figure out. Any help at all would be great!

[EDIT] so a new small issue has come up. Now with camera will rotate in one direction but it does not follow the player. apparently setting the position within the update helps it move with the player. back to square 1 I suppose

Simplified the existing code a bit to help understand it.

``````Camera camera = Camera.main;

// Set the position of the camera on the x-z plane to:
// distance meters behind the target
camera.transform.position = target.position;
camera.transform.position -= cameraRotation * distance;

// Set the height of the camera
camera.transform.position = new Vector3(camera.transform.position.x, target.position.y + height, camera.transform.position.z);

// Always look at the target
camera.transform.LookAt(target);

if (Input.GetAxis("Horizontal") > 0.0f)
{
camera.transform.RotateAround(target.position, Vector3.up, 30 * Time.deltaTime);
}
``````

So now the code is a bit more readable. Still having the issue where the camera will only barely move. I think setting the transform.position to the target.position every time this runs is messing with that. But if I move that to the Start() function like I did earlier, the camera does not move with the player. So I am not really sure what to do

I finally got it working, for the most part. Went in a slightly different direction and did this:

``````Camera camera = Camera.main;
camera.transform.position = new Vector3(target.position.x + cameraOffset.x, target.position.y + height, target.position.z + cameraOffset.z);

// Always look at the target
camera.transform.LookAt(target);

if (Input.GetAxis("Horizontal") > 0.0f)
{
camera.transform.RotateAround(target.position, Vector3.up, 30 * Time.deltaTime);
cameraOffset = transform.position - target.transform.position;
}
``````

This seems to work for the most part. Will need to adjust some parameters but the basics are all there. Last part I need is the camera is only able to move in one direction, so I am not sure how to set it up so when I press the left arrow the camera moves to the left, and the right arrow moves to the right.

Alright I got the left/right rotation to work

``````Camera camera = Camera.main;
camera.transform.position = new Vector3(target.position.x + cameraOffset.x, target.position.y + height, target.position.z + cameraOffset.z);

// Always look at the target
camera.transform.LookAt(target);

if (Input.GetAxis("Horizontal") > 0.0f || Input.GetAxis("Horizontal") < 0.0f)
{
camera.transform.RotateAround(target.position, -Vector3.up * Input.GetAxis("Horizontal"), 30 * Time.deltaTime);
cameraOffset = transform.position - target.transform.position;
}
``````

so next is working with the zoom, and the up/down