Now it's rotating in space i can rotate the camera around but not around the player. I want the camera to be rotating only around the player.
using UnityEngine;
using System.Collections;
public class CameraMover : MonoBehaviour
{
public float speedH = 2.0f;
public float speedV = 2.0f;
private float yaw = 0.0f;
private float pitch = 0.0f;
public Transform playerTransform;
public Transform mainCameraTransform = null;
private Vector3 cameraOffset = Vector3.zero;
public float turnSpeed = 3;
void Start()
{
mainCameraTransform = Camera.main.transform;
//Get camera-player Transform Offset that will be used to move the camera
cameraOffset = mainCameraTransform.position - playerTransform.position;
}
void LateUpdate()
{
//Move the camera to the position of the playerTransform with the offset that was saved in the begining
mainCameraTransform.position = playerTransform.position + cameraOffset;
yaw += speedH * Input.GetAxis("Mouse X");
pitch -= speedV * Input.GetAxis("Mouse Y");
mainCameraTransform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
}
}
I'm using now eulerAngles for the rotation.
As mentioned in the comments you need to create a parent object for the camera, and rotate that object instead of the camera. Try this code:
using UnityEngine;
using System.Collections;
public class CameraMover : MonoBehaviour
{
public float speedH = 2.0f;
public float speedV = 2.0f;
private float yaw = 0.0f;
private float pitch = 0.0f;
public Transform playerTransform;
public Transform mainCameraTransform = null;
private Vector3 cameraOffset = Vector3.zero;
public float turnSpeed = 3;
// Create a camera parent object
GameObject cameraParent;
void Start()
{
cameraParent = new GameObject();
mainCameraTransform = Camera.main.transform;
//Get camera-player Transform Offset that will be used to move the camera
cameraOffset = mainCameraTransform.position - playerTransform.position;
// Position the camera parent to the player and add the camera as a child
cameraParent.transform.position = playerTransform.position;
mainCameraTransform.parent = cameraParent.transform;
}
void LateUpdate()
{
//Move the camera to the position of the playerTransform with the offset that was saved in the begining
//mainCameraTransform.position = playerTransform.position + cameraOffset;
yaw += speedH * Input.GetAxis("Mouse X");
pitch -= speedV * Input.GetAxis("Mouse Y");
// Rotate the camera parent instead of the camera
cameraParent.transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.