Camera Rotates when player rotates
Hi. I was using this code to rotate the camera when the player rotates:
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
public GameObject player;
private Vector3 offset;
// Use this for initialization
void Start () {
offset = transform.position - player.transform.position;
}
// Update is called once per frame
void LateUpdate () {
transform.position = player.transform.position + offset;
transform.rotation = player.transform.position;
transform.Rotate (new Vector3 (45, 00));
}
}
but the player wasn't the axis of rotation. So someone gave me this:
public class CameraController : MonoBehaviour
{
public GameObject player;
private Vector3 offset;
void Start ()
{
offset = transform.position - player.transform.position;
}
void LateUpdate ()
{
//transform.position = player.transform.position + offset;
transform.RotateAround (player.transform.position, Vector3.up, 100 * Time.deltaTime );
//transform.Rotate (new Vector3 (45, 0, 0));
}
}
yet all this does is add a Star Wars like flyby of the player. How can i get the camera to rotate with the player, yet keeping them as the axis of rotation?
@Tymewiz thanks for the code and hope you can help with this one.
Comment
Answer by Tymewiz · Mar 20, 2016 at 11:43 AM
while i am not sure what you want, if you want something that is based on GTA-V while driving first create an empty gameobject and make it and the camera a child of the player and the empty gameobject is behind the player then add this script to the camera:
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour
{
float verticalinput;
float horizontalinput;
[Range(0,1)]
public float sensitivity = 0.001f;
bool ijklpressed;
float snappingdelay;
public Transform Player;
public Transform EmptyGameobject;
//setting up input, could skip this step by setting the equivalent up in the input manager and setting horizontalinput
// and vertinput equal to the axisname that was set up.
//i'm doing it this way because you might already be using input.getaxis("Horizontal/vertical");
// this setup uses IJKL
void Update ()
{
if (Input.GetKey (KeyCode.J)) {
horizontalinput -= sensitivity;
ijklpressed = true;
}
if (Input.GetKey (KeyCode.L)) {
horizontalinput += sensitivity;
ijklpressed = true;
}
if (Input.GetKey (KeyCode.I)) {
verticalinput += sensitivity;
ijklpressed = true;
}
if (Input.GetKey (KeyCode.K)) {
verticalinput -= sensitivity;
ijklpressed = true;
}
if (Mathf.Abs (verticalinput) < Mathf.Epsilon) {
verticalinput = 0;
}
if (Mathf.Abs (horizontalinput) < Mathf.Epsilon) {
horizontalinput = 0;
}
if (!ijklpressed) {
snappingdelay += Time.deltaTime;
if (snappingdelay >= .25f) {
horizontalinput /= 1.01f;
verticalinput /= 1.01f;
}
} else {
ijklpressed = false;
snappingdelay = 0;
}
horizontalinput = Mathf.Clamp (horizontalinput, -1, 1);
verticalinput = Mathf.Clamp (verticalinput, -1, 1);
}
void LateUpdate ()
{
transform.LookAt (Player);
transform.localPosition = EmptyGameobject.localPosition + new Vector3 (horizontalinput, verticalinput, 0);
}
}