Hey all, first and foremost; I’d like to apologise incase I’ve submitted this in the wrong thread. Secondly, I’d like to add; I’m new to scripting in C# and using Unity although I have used other languages and editors briefly. Finally; I’d like to thank you for reading this!
I’m trying to make a game with an isometric perspective. What I’m hoping to have answered is; how can a make the camera rotate around the player? I’ve followed a few tutorials here and there. Firstly I used this code for my players movement (matching the players movement with the camera angle).
(A few notes to add, ignore any code involving the “secondCamera” variable, that’s in there purely to test some other related stuff. My isometric projection is on the "firstCamera’ that has a rotation of (x=30, y=45, x=0).)
This is code attached to my player object.
[SerializeField] float moveSpeed = 4f;
[SerializeField] public Camera firstCamera;
[SerializeField] public Camera secondCamera;
Vector3 forward, right;
void Update()
{
if (firstCamera.enabled == true)
{
forward = firstCamera.transform.forward;
forward.y = 0;
forward = Vector3.Normalize(forward);
right = Quaternion.Euler(new Vector3(0, 90, 0)) * forward;
}
else if (secondCamera.enabled == true)
{
forward = secondCamera.transform.forward;
forward.y = 0;
forward = Vector3.Normalize(forward);
right = Quaternion.Euler(new Vector3(0, 90, 0)) * forward;
}
if (Input.anyKey)
{
Move();
}
}
void Move()
{
Vector3 direction = new Vector3(Input.GetAxis("HorizontalKey"), 0, Input.GetAxis("VerticalKey"));
Vector3 rightMovement = right * moveSpeed * Time.deltaTime * Input.GetAxis("HorizontalKey");
Vector3 upMovement = forward * moveSpeed * Time.deltaTime * Input.GetAxis("VerticalKey");
Vector3 heading = Vector3.Normalize(rightMovement + upMovement);
transform.forward = heading;
transform.position += rightMovement;
transform.position += upMovement;
}
After using some of this code, I then wanted my camera to follow the player. The issue I found with that was, when I make the camera a child of the player; every time I moved my player in a direction, it changed the camera’s forward face. So I scoured the internet (Google) for an answer and found this code!
This code is attached to my Camera (firstCamera) object.
[SerializeField]
Transform target;
[SerializeField]
float smoothing = 5f;
Vector3 offset;
void Start()
{
offset = transform.position - target.position;
}
void LateUpdate()
{
Vector3 targetCamPos = target.position + offset;
transform.position = Vector3.Lerp(transform.position, targetCamPos, smoothing * Time.deltaTime);
}
Now this code corrected all the issues with following the player (without the need to child my camera object to the player). However, this code is also responsible for the next code not working correctly.
[SerializeField] public float rotspeed;
[SerializeField] public GameObject target;
void Update()
{
transform.RotateAround(target.transform.position, Vector3.up, rotspeed * Time.deltaTime);
}
So… With all that out of the way. I want to rotate my camera around the player as you would expect the “transform.RotateAround” to work.
Without the second section of code in this post; it works exactly how I want it to (well, for now anyway). If somebody could provide some information as to why this doesn’t work the way an “idiot” like me expects it to; that would be awesome! I could also do with someone pointing me in the right direction for a fix? I don’t necessarily want someone to code it for me; but a C# example of working code and a basic explanation of why it works would be highly appreciated too!