how do i move a camera with mouse.

I’m new to unity. I started with the roll a ball tutorial. I finished the tutorial and I wanted to add on to it. I am completely stuck with this. the camera follows the ball but I want to control the rotation with the mouse to. I tried to use the code for moving the ball but used rotation instead of movement and used Mouse X and Mouse Y, which is supposed to be the controls for the mouse. what do I need to do?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraControler : MonoBehaviour {

public GameObject player;
public float sensitivity;

private Vector3 offset;
private Rigidbody rb;

void Start () 
{
	offset = transform.position - player.transform.position;
	rb = GetComponent<Rigidbody> ();
}

void LateUpdate () 
{
	transform.position = player.transform.position + offset;
}

void FixedUpdate ()
{
	float rotateHorizontal = Input.GetAxis ("Mouse X");
	float rotateVertical = Input.GetAxis ("Mouse Y");

	Vector3 rotation = new Vector3 (rotateHorizontal, 0.0f, rotateVertical);

	rb.AddForce (rotation * sensitivity);

}

}

Hello!
I think the issue here is that you’re using rigidbody with a camera, which (at least in my tests) doesn’t work. Instead you want to change the transform, instead of going through the rigidbody.

public GameObject player;
	public float sensitivity;

	void FixedUpdate ()
	{
		float rotateHorizontal = Input.GetAxis ("Mouse X");
		float rotateVertical = Input.GetAxis ("Mouse Y");
		transform.RotateAround (player.transform.position, -Vector3.up, rotateHorizontal * sensitivity); //use transform.Rotate(-transform.up * rotateHorizontal * sensitivity) instead if you dont want the camera to rotate around the player
		transform.RotateAround (Vector3.zero, transform.right, rotateVertical * sensitivity); // again, use transform.Rotate(transform.right * rotateVertical * sensitivity) if you don't want the camera to rotate around the player
	}

Hope this helps!

Edit: I forgot to mention if you don’t want the camera to rotate around the player, you should also keep the offset code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public GameObject player;
public float sensitivity;
 
void FixedUpdate (){
    float rotateHorizontal = Input.GetAxis ("Mouse X");
    float rotateVertical = Input.GetAxis ("Mouse Y");
    transform.RotateAround (player.transform.position, -Vector3.up, rotateHorizontal * sensitivity);
    transform.RotateAround (Vector3.zero, transform.right, rotateVertical * sensitivity);
}

doesn’t work please help me


it gives me this error @SneakyLeprechaun

hey im trying to get my camera to move 360 in the play veiw this is what i have.

public class CameraFollow: MonoBehaviour
{
public GameObject player;
public float sensitivity;

private Vector3 offset;
private Rigidbody rb;

void Start ()
{
offset = transform.position - player.transform.position;
rb = GetComponent ();
}

void LateUpdate ()
{
transform.position = player.transform.position + offset;
}

void FixedUpdate ()
{
Debug.Log("horizontal rotation = " + Input.GetAxis (“Mouse X”)); Debug.Log("Vertical rotation = " + Input.GetAxis (“Mouse Y”));
float rotateHorizontal = Input.GetAxis (“Mouse X”);
float rotateVertical = Input.GetAxis (“Mouse Y”); Vector3 rotation = new Vector3 (rotateHorizontal, 0.0f, rotateVertical); rb.AddForce (rotation * sensitivity);

}

am I missing something or because it giving me the debug log but the camera is not moving?

using UnityEngine;
public Camera playerCamera;
public float lookSpeed = 2.0f;
public float lookXLimit = 45.0f;
float rotationX = 0;

  void Update()        
  {
         rotationX += -Input.GetAxis("Mouse Y") * lookSpeed;
         rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit);
         playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
         transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0);
  }