I have searched everywhere for a mouse controlled camera that turns the player left or right based on horizontal mouse movement and looks up and down based on vertical mouse movement.
Any online script or tutorial I find only turns the player left or right.
A good example of the camera style I want is 3rd Person Skyrim, 3rd person Minecraft, or even just a 1st person camera for 3rd person.
I’m new to c# code so I don’t really understand the code, know how to edit the code, or write my own. (I’m used to python coding)
This is the camera script I’m currently using:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseAimCamera : MonoBehaviour
{
public GameObject target;
public float rotateSpeed = 5;
Vector3 offset;
void Start()
{
offset = target.transform.position - transform.position;
}
void LateUpdate()
{
float h = Input.GetAxis("Mouse X") * rotateSpeed;
target.transform.Rotate(0, h, 0);
float desiredAngle = target.transform.eulerAngles.y;
Quaternion rotation = Quaternion.Euler(0, desiredAngle, 0);
transform.position = target.transform.position - (rotation * offset);
transform.LookAt(target.transform);
}
}
You can use the “multipurpose camera rig” in standard assets. Attach the “free look cam” script in standard assets to the parent of the camera. Then, in your character controller script, add this line:
Character.eulerAngles = new Vector3(0.0f, (GameCamera.eulerAngles.y), 0.0f);
Assign Character and GameCamera in the inspector and you’re done. You may need to tweak a couple other things. It’s been a while since I did this.
There is a script called “free look cam” that’s in /Standard Assets/Cameras/scripts. Drag it to MultipurposeCameraRig in the inspector. Then, create a new script called “mouse_move” and attach it to your player. Copy this into mouse_move:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class mouse_move : MonoBehaviour
{
[SerializeField] private Transform player;
[SerializeField] private Transform cam;
private void turn_player()
{
player.eulerAngles = new Vector3(0.0f, cam.eulerAngles.y, 0.0f);
cam.transform.position = player.transform.position;
}
private void Update()
{
turn_player();
}
}
In the inspector, you’ll see two new fields pop up under the new script. Drag “MultipurposeCameraRig” to “cam” and your character to “player.” That should do it. Good luck.