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.
there are definitely other ways of doing this I'm sure but this is just a jumpoff point.
– bitthebillias