- Home /
Automatic 3rd person camera
I am trying to make a 'tank control style game and am working on the camera. I would like for it to be automatic like in older games.
Here is what I am working with:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class Main_Camera_Controller : MonoBehaviour
{
public GameObject Camera;
public Vector3 _offset;
public Vector3 rawInputMovement;
public float speed;
[Range(0.01f, 1.0f)]
public float SmoothFactor;
private void Start()
{
_offset = Camera.transform.position - this.transform.position;
}
void LateUpdate()
{
if (rawInputMovement.x < 0)
{
Quaternion camTurnAngle = Quaternion.AngleAxis(transform.rotation.y * speed * Time.deltaTime, Vector3.up);
_offset = camTurnAngle * _offset;
}
else if (rawInputMovement.x > 0)
{
Quaternion camTurnAngle = Quaternion.AngleAxis(-transform.rotation.y * speed * Time.deltaTime, Vector3.down);
_offset = camTurnAngle * _offset;
}
else
{
// get camera to be centered on the player again somehow...
}
Vector3 newPosHorizontal = transform.position + _offset;
Camera.transform.position = Vector3.Lerp(Camera.transform.position, newPosHorizontal, SmoothFactor/Time.deltaTime);
Camera.transform.LookAt(this.transform);
}
public void onMovement(InputAction.CallbackContext value)
{
Vector2 inputMovement = value.ReadValue<Vector2>();
rawInputMovement = new Vector3(inputMovement.x, 0, inputMovement.y);
}
}
I am not sure as to how to get the camera to move back behind the player when moving forward.
The way I am doing it is dependant on the player's rotation at the moment and probably not the best approach? I originally had it set by using the input values from a gamepad but this seemed to be more akin to what I am aiming for.
I am not sure; this is my first dive into how cameras work. Any info would be appreciated.
Answer by bitthebillias · May 11, 2021 at 09:23 PM
just posting this for others to find useful.
This is my solution for a tank-like automatic camera:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class Main_Camera_Controller : MonoBehaviour
{
public GameObject Camera;
public Transform DefaultPos;
public Vector3 _offset;
public Vector3 rawInputMovement;
public float speed;
[Range(0.01f, 5.0f)]
public float SmoothFactor;
public bool Collided;
private void Start()
{
_offset = Camera.transform.position - this.transform.position;
}
private void LateUpdate()
{
Vector3 CamRotation = _offset;
if(rawInputMovement.x < 0)
{
Quaternion camTurnAngle = Quaternion.AngleAxis(rawInputMovement.x * speed * Time.deltaTime, Vector3.up);
CamRotation = camTurnAngle * CamRotation;
}
if (rawInputMovement.x > 0)
{
Quaternion camTurnAngle = Quaternion.AngleAxis(-rawInputMovement.x * speed * Time.deltaTime, Vector3.down);
CamRotation = camTurnAngle * CamRotation;
}
if (Collided)
{
//cam pos to collision point
}
else
{
Camera.transform.position = Vector3.Lerp(Camera.transform.position, DefaultPos.position, SmoothFactor * Time.deltaTime);
}
Camera.transform.LookAt(transform);
}
public void onMovement(InputAction.CallbackContext value)
{
Vector2 inputMovement = value.ReadValue<Vector2>();
rawInputMovement = new Vector3(inputMovement.x, 0, inputMovement.y);
}
}
Basically, you have a point(DefaultPos) that the camera(Camera) is always trying to reach via lerping
The rotation comes in by calculating the offset then multiplying the current camera rotation angle by the offset. (I am no expert on this by any means) This makes it sway the direction you are rotating in.
there are definitely other ways of doing this I'm sure but this is just a jumpoff point.