- Home /
How to move camera back to rest position after panning around character?
Me and my friends are building a third person android game. After scouring the internet i have not been able to find what I am looking for. The closest description or rather the most accurate description of a camera control is GTA V camera control, in which when the user starts running and moving around the camera slowly floats behind the character while still giving the character the ability to freely rotate the camera to look behind him or around him.
After many attempts this is how far i got,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test_CameraBehaviour : MonoBehaviour
{
public Transform Player;
public Transform LookAt;
public Transform camTransform;
public Camera cam;
public Vector3 offset = new Vector3 (0f, 0f, 4f);
private float currentX = 0f;
private float currentY = 0f;
public Vector2 maxY = new Vector2(0, 10);
public float sensitivityY = 1f;
void Start()
{
camTransform = transform;
cam = GetComponent<Camera>();
Player = GameObject.Find("TEST_Player").transform;
LookAt = new GameObject("lookAt").transform;
}
void Update()
{
currentX += Input.GetAxis("Mouse X");
currentY += Input.GetAxis("Mouse Y");
currentY = Mathf.Clamp(currentY, maxY.x, maxY.y);
LookAt.position = Player.position + new Vector3(0f, 1.5f, 0);
}
void LateUpdate()
{
Quaternion rotation = Quaternion.Euler(currentY, currentX, 0);
camTransform.position = LookAt.position + rotation * offset;
camTransform.LookAt(LookAt);
}
}
This is with heavy reference to different posts and videos. Now this camera follows the player around but does not remain behind the player (or doesn't look in the direction the player is looking).
I have a few ideas on how to implement a bool function to detect the player finger slide to engage a "free camera movement". However i have no clue how to set this up.
EDIT : Things that i would like to achieve:
Stay behind character while character is moving
Allow free camera movement (player over ride camera controls??)
Float the camera back to rest position behind character if not in free camera movement mode (at a set speed rotating around the player, not over or below the player. depending on position must detect the shortest path to rest position)