Isometric Follow Camera which moves to Position through key inputs.

Hello.
I’ve been working on this problem for most of the day scouring the internets for a solution to this problem with little luck. Hopefully the fantastic people here will be able to help

``````public class CameraFollow : MonoBehaviour

{
public GameObject target = null;

private Vector3 positionOffset = Vector3.zero;

void Start ()

{

positionOffset = transform.position - target.transform.position;
}

void Update ()

{

if (target != null)
{
transform.LookAt (target.transform);

transform.position = target.transform.position + positionOffset;
}

}
}
``````

Essentially what I want to do is to be able to hit a key (say ‘Q’ for example) and the camera moves along to a position 90 degrees to the left and stays there till I hit ‘Q’ again where it moves another 90 degrees. If I were to hit ‘Q’ four times consecutively it would bring the camera to its original position. Same thing if I were to hit ‘E’ it would bring the camera to the right and so on.

I hope I’m explaining it right.
Please if someone has a solution that would be great, thanks!

Make the camera a child of the object and then use this method.

• Set a variable that you will store the amount of degrees to rotate (should be a private float, I called it ‘targetAngle’).
• Create two Ifs (one for Q one for E). Q should be a negative value, and E a positive value. Set the float variable to -=, += this value respectively.
• Tell the program that if your float variable is not equal to ‘0’ than Rotate()
• Create the Rotate() method and give it two ifs as well.
• The first if deals with the float being >0 the second <0. For both use "transform.RotateAround(transform.pos point in space, Vector3.up axis in space because you want to rotate around the y-axis in isometric, angle in which you want it to rotate I created another var for this called ‘rotationAmount’)
• Under this argument for both if statements you then tell it to -= or +=, depending on which direction you are rotating, to the private float you created at the beginning.

Hope that helps.

It helps loads, thanks. Obviously I’m new to coding so a visual of the code with some notes will be wonderful, otherwise I’ll just head down and power through this explanation till I figure it out.
Thanks

If you can’t figure it out then I’ll help code some of it but try to look up the API with the commands I’ve showed you.

1 Like

Well, been knocking on the code for some time. Took a long time to get the code working without any errors, heh. So far, this is what I have and it works until I hit Q or E then the camera just goes a little crazy.
Here is what I got:

``````using UnityEngine;
using System.Collections;

public class RotationalCamera : MonoBehaviour {

private float targetAngle;

void Start ()
{

}

void Update ()
{
if (Input.GetKey(KeyCode.Q))
targetAngle -= 1 ;
if (Input.GetKey(KeyCode.E))
targetAngle += 1 ;
if (targetAngle != 0)
Rotate() ;
}

private float Rotate ()
{
if (targetAngle > 0)
transform.RotateAround(transform.position, Vector3.up, +90);
if (targetAngle < 0)
transform.RotateAround(transform.position, Vector3.up, -90);
return 0;
}
}
``````

Keep the target angle to 90, I’ll post up my code in a few seconds here.

``````void Update()
public GameObject targetObject;
private Vector3 playerPos;
private float targetAngle = 0;
const float rotationAmount = 1.5f;
public float rDistance = 1.0f;
public float rSpeed = 1.0f;

void Update() {

playerPos.x = targetObject.transform.position.x;
playerPos.y = targetObject.transform.position.y;

// Trigger functions if Rotate is requested
if (Input.GetKeyDown(KeyCode.Q)) {
targetAngle -= 90.0f;
}
if (Input.GetKeyDown(KeyCode.E)) {
targetAngle += 90.0f;
}

if(targetAngle !=0)
{
Rotate();
}
}

protected void Rotate()
{

float step = rSpeed * Time.deltaTime;
float orbitCircumfrance = 2F * rDistance * Mathf.PI; // Makes the camera orbit based on circle
float distanceDegrees = (rSpeed / orbitCircumfrance) * 360; // Distance
float distanceRadians = (rSpeed / orbitCircumfrance) * 2 * Mathf.PI; // Arclength

if (targetAngle>0)
{
transform.RotateAround(playerPos, Vector3.up, -rotationAmount);
targetAngle -= rotationAmount;
}
else if(targetAngle <0)
{
transform.RotateAround(playerPos, Vector3.up, rotationAmount);
targetAngle += rotationAmount;
}

}
}
``````

I should’ve said “OR” use this method when speaking before.

Above is a more complex way of doing it if you don’t want to child your camera (usually for RTS this is the case).

If you don’t mind doing that the other way to do it is to child the camera to the object that is the focal point (player character) and use this code:

``````private float targetAngle = 0;
const float rotationAmount = 1.5f;
public float rSpeed = 1.0f;

void Update () {
// Rotation method
if (Input.GetKeyDown(KeyCode.Q)) {
targetAngle -= 90.0f;
}
if (Input.GetKeyDown(KeyCode.E)) {
targetAngle += 90.0f;
}

if(targetAngle !=0)
{
Rotate();
}
}

protected void Rotate()
{

if (targetAngle>0)
{
transform.RotateAround(transform.position, Vector3.up, -rotationAmount);
targetAngle -= rotationAmount;
}
if(targetAngle <0)
{
transform.RotateAround(transform.position, Vector3.up, rotationAmount);
targetAngle += rotationAmount;
}

}
``````

You don’t “have” to use a protected void btw, just kept it for continuity sakes.