Hi all, Been using Unity for a couple of months now and feel comfortable with all the scripts I've written currently in my game. However, I've just gone back to the camera script I hobbled together and want to make some adjustments, but can't make total sense of what is there. The main adjustment I'd like to make is to change the angle of the camera. Anyway, here is my script, if anyone can advise, much appreciated. Code (CSharp): using UnityEngine; using System.Collections; public class MainCamera : MonoBehaviour { public GameObject Target; // The target in which to follow (player in this case) public Vector3 CameraOffset = new Vector3(0,10,-20); // This will allow us to offset the camera for the player's view. public float CameraSpeed = 10f; //How fast the camera will track the target private Camera _camera; // Use this for initialization void Start () { _camera = GetComponent<Camera>(); } // Update is called once per frame void FixedUpdate () { // Check to see if the camera and the target exist if (_camera !=null && Target != null) { Vector3 targetPos = Target.transform.position; //target coordinates Vector3 offset = CameraOffset; float cameraAngle = _camera.transform.eulerAngles.y; //camera angle on the y axis float targetAngle = Target.transform.eulerAngles.y; //target facing angle on the y axis targetAngle = cameraAngle; // Adjust camera angle base on which way player is facing targetAngle = Mathf.LerpAngle(cameraAngle, targetAngle, CameraSpeed * Time.deltaTime); //LerpAngle is smooth offset = Quaternion.Euler (0,targetAngle,0) * offset; _camera.transform.position = Vector3.Lerp (_camera.transform.position, targetPos + offset, CameraSpeed * Time.deltaTime); //_camera.transform.LookAt (targetPos); //rotates camera towards targetPos } } }
I advice checking the API, so you understand that code before trying to change / modify something in it. Not that noone wants to help you, but we can only explain how to change things but not understand it for you, which should be your first goal you wanna reach As your code is documented in a good way, you could just try and error some things to see, how the script works.
That camera script is a good starting point. I'm not sure what exactly you mean by change the angle of the camera. Do you want to make it look up/down? Left/right? You can accomplish all of this by changing the rotation of the camera's transform on a certain axis. Something like this... Code (CSharp): //Inside FixedUpdate if (Input.GetButton("N")) { //Rotate the camera on the Y axis //You can use transform.Rotate for this } I can't really test out code right now since I'm at work, but using transform.Rotate and passing it in the amount to rotate on the given axis will accomplish what you need.
Thanks Limeoats. That pointed me in the right direction, however, the camera now rotates continuiously. I wanted to just change the static angle of the camera.
Ok, I've been quite silly. I forgot that I can just change the static angle of the camera, or any gameobject for that matter, in the gameobject inspector. Was interesting to know how to change it dynamically though, so thanks.
No problem, glad I can help! I'm actually in the process of implementing my dynamic camera controls, so if you have any questions, let me know!