Add camera rotation to the player camera prefab

I added mouse move camera rotation to the player camera prefab.
Drag the player onto the hierarchy expand and click the camera then add a script (PlayerCamera.cs). Remember to apply in the inspector.
Then you can delete the temporary player off the hierarchy.
In your c# editor edit PlayerCamera.cs and you and paste the code included in this post.
Now when you play you can use the mouse to rotate around the horizon like in ZombieRunner.
This way if you move past the ship you can rotate around and find the ship and not feel so lost at sea.

//Start PlayerCamera.cs
using UnityEngine;
using System.Collections;

public class PlayerCamera : MonoBehaviour {
//http://gamedev.stackexchange.com/questions/104693/how-to-use-input-getaxismouse-x-y-to-rotate-the-camera
public float speedH = 2.0f;
public float speedV = 2.0f;
private float yaw = 0.0f;
private float pitch = 0.0f;

// Use this for initialization
void Start () {
    Debug.Log("PlayerCameraStart ");
}

// Update is called once per frame
void Update () {
    yaw += speedH * Input.GetAxis("Mouse X");
    pitch -= speedV * Input.GetAxis("Mouse Y");

    transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);

}

}
//end PlayerCamera.cs

1 Like

Nice, I did a similar thing, although I added the functionality to the player instead.

It just more relative, as though the camera was fixed, and it was the player rotating/truning instead.

Privacy & Terms