- Home /
Third Person Camera
Hello there, i'm writing a third person camera for game at the moment. I want the camera to moving through the terrain and move over objects instead of clipping. Giving the camera a rigidbody ended up a special kind of weird. What do you recommend?
Greets :)
Answer by Paulit0 · Aug 18, 2017 at 10:09 AM
@JannickL I've done a third person camera thing that works well but only in solo player. What you have to do is making the camera child of your player gameObject, place it where you want, then use this script to rotate the player game object with Mouse movement
using UnityEngine;
using System.Collections;
public class EntityMouseRot : MonoBehaviour {
float verticalSpeed = 10.0f; //Mouse sensivity
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Rotate(0, verticalSpeed * Input.GetAxis("Mouse X"),0);
}
}
This will rotate the player gameObject and thus the camera attached to it. If your goal was to make the camera rotate independently of your player gameObject, then simply parent your Camera to an empty gameObject that follows the player gameObject (this would be the script for the empty game object):
using UnityEngine;
using System.Collections;
public class EntityMouseRot : MonoBehaviour {
float verticalSpeed = 10.0f;
public Transform Player; //Drag the player gameObject here it will get its transform component
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.position = Player.position;
transform.Rotate(0, verticalSpeed * Input.GetAxis("Mouse X"),0);
}
}
This script will make the empty gameObject follow the player position but rotate independently, with mouse horizontal movement.