Question by
chrisw90uk · May 07, 2017 at 12:46 PM ·
cameracamera movementcamera rotation
Edit Z position of third-person style camera
Hello,
I'm new to Unity (like, a day's experience). I've created a camera that follows and rotates a player object with the mouse from an existing tutorial which works great. However, I'd like to be able to modify the distance of the camera from the player and also the X rotation. I've been tinkering with the values to see if I can figure it out but I've had little luck. Any help would be appreciated :)
Here's my camera script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class camCtrl : MonoBehaviour {
//camera pos rel to player
public GameObject player;
private Vector3 offset;
public float mouseSpeed = 10f;
// Use this for initialization
void Start () {
//set position to player offset
offset = transform.position - player.transform.position;
//lock cursor to middle
Cursor.lockState = CursorLockMode.Locked;
Cursor.lockState = CursorLockMode.None;
Cursor.visible = false;
}
// Update is called once per frame
void LateUpdate () { //runs every frame but guaranteed to run after all update functions
//rotate player character
float mouseX = Input.GetAxis("Mouse X") * mouseSpeed;
player.transform.Rotate(0, mouseX, 0);
//rotate camera relative to player
float angle = player.transform.eulerAngles.y;
Quaternion rotation = Quaternion.Euler(-10, angle, 0);
transform.position = player.transform.position + (rotation * offset);
transform.LookAt(player.transform);
}
}
Comment
Your answer
Follow this Question
Related Questions
Maximum Camera Angle Rotation 0 Answers
Camera rotation 2 Answers
Make camera follow and look at object at the same time 0 Answers
Camera rotation problem: Keeps reseting to initial position 0 Answers
Camera rotation problem 0 Answers