- Home /
How do I rotate the camera around the player character with keys A and D while keeping him in the center while using the mouse for facing?
Hi there, I am new to working with Unity and am currently working on a class project.
My current issue is camera control, what I want is to have the camera locked to the character's transforms and leave the camera rotation up to the player using A and D. So far the last few hours spent searching for an answer mostly end up with parenting an empty object to the character and having the camera target the empty object. This results in everything working the way I want except that the character is at the very bottom of the screen, and any offsets I make change the camera pivot. The other popular solution was to parent the camera to the character however that wont work for me since the character's facing is controlled by the mouse position, so when I use this method the camera spins violently whenever it isn't centered.
So far this is what I have for the camera script.
var character : Transform;
var height : float = 200;
var speed : float = 60;
function Update () {
var orb = Input.GetAxis("Horizontal")*speed;
transform.RotateAround(character.position,Vector3.up,orb*Time.deltaTime);
transform.position.x = character.transform.position.x;
transform.position.z = character.transform.position.z;
transform.position.y = character.transform.position.y + height;
}
The camera is supposed to be zoomed out and looking down at a 45-ish degree angle at the character.
Any help is appreciated, I feel like there's something very simple I'm missing. JAVAAAAAAAAAAA!
Thanks.
Answer by Clet_ · May 01, 2014 at 01:28 AM
Here's a small script that you can attach to your camera.
using UnityEngine;
public class RotateCamera : MonoBehaviour {
public Transform target;
public float angularSpeed;
[SerializeField][HideInInspector]
private Vector3 initialOffset;
private Vector3 currentOffset;
[ContextMenu("Set Current Offset")]
private void SetCurrentOffset () {
if(target == null) {
return;
}
initialOffset = transform.position - target.position;
}
private void Start () {
if(target == null) {
Debug.LogError ("Assign a target for the camera in Unity's inspector");
}
currentOffset = initialOffset;
}
private void LateUpdate () {
transform.position = target.position + currentOffset;
float movement = Input.GetAxis ("Horizontal") * angularSpeed * Time.deltaTime;
if(!Mathf.Approximately (movement, 0f)) {
transform.RotateAround (target.position, Vector3.up, movement);
currentOffset = transform.position - target.position;
}
}
}
There is a little bit of preparation for it to works though.
After the script has been attached, drag whatever you want to be followed inside the target field of the script.
Move the camera around in the side and position it as you want it to be in-game.
Click on the small gear symbol on the upper right hand side of the script's inspector and click on "Set Current Offset"
Press play and enjoy
Sorry if the script is in C#, I have absolutely no clue how to code in UnityScript. Hope you can figure it out
I got it to work! Now I know a little bit more about C#, thank you!
Do you know how I could set this code to work for specific buttons. For example I would like to set the O key to rotate left and the P key to rotate right. I too am a beginner in Unity.
Your answer
Follow this Question
Related Questions
Cinemachine not working with my Movement Script 0 Answers
how do i make first person character rotate left and right along with camera? 0 Answers
Want to rotate camera horizontally around object using left and right arrow keys 0 Answers
move the object where camera look 0 Answers
Input.mousePosition for new networking 0 Answers