- Home /
How do I make a 3d person camera that changes with keyboard based input
I want to make a script that I could move my camera by doing things like pressing the x or c button. for example lets say the camera is looking north towards the player, I want the camera to look west and I would do that by pressing x or if I wanted it to look towards south so I would press c. How do I do that?
using UnityEngine;
using System.Collections;
public class CameraControll : MonoBehaviour
{
public Transform lookAt;
public Transform camtransform;
private Camera cam;
private float Distance = 10f;
private float currentX = 0f;
private float currentY = 0f;
private float sesivityX = 4f;
private float sesivityY = 1f;
private void Start()
{
camtransform = transform;
cam = Camera.main;
}
private void Update()
{
currentX += Input.GetAxis("Mouse X");
currentY += Input.GetAxis("Mouse Y");
}
private void LateUpdate()
{
Vector3 dir = new Vector3(0, 0, -Distance);
Quaternion rotation = Quaternion.Euler(currentY, currentX, 0);
camtransform.position = lookAt.position + rotation * dir;
camtransform.LookAt(lookAt.position);
}
}
Answer by Creeper_Math · Dec 18, 2016 at 11:24 AM
A good setup for this would be a bunch of locations for the camera, and a script that (I would suggest) have a smooth pan...
This will not work initially.. What you will need to make sure is that the gameobejct[] arrays are set to a length of 3. Then you will need to make a set of empty gameobjects that will be the location variables. Suggested to be named "CameraLocation1" "CameraLookAt1" "CameraLocation2" "CameraLookAt2" etc (need to be 3 for the example below)... and put them into the arrays accordingly ("CameraLocation1" goes under CameraGoalPosition at entry "0" as well as "CameraLookAt1" under CameraGoalLookAt, etc...) For each one you will need to put then under the player gameobject, or another gameobject that has a script to set it's position to the player's position ( transform.position = player.transform.position ), which has all the objects as children of it (so the positions move relatively to the player's location)
Each object pair "CameraLocation1" "CameraLookAt1" represents a camera phase / position. For your example and what I sorta have put into the code, the following sets would work with the pairs
CameraLocation1 and CameraLookAt1 represent the position and lookat of the camera when the player wants to look north at the player (put location behind camera and lookat infront of camera CameraLocation2 and CameraLookAt2 represent looking west CameraLocation3 and CameraLookat3 represent looking east
public GameObject[] CameraGoalLookAt;
public GameObject[] CameraGoalPosition;
public Vector3 CameraCurrentLookAt;
public int selection;
public float MovementDividor;
public float LookAtDividor;
// The location setup
void Update() {
if(Input.GetKeyDown(KeyCode.X)) {
selection = 1;
}
if(Input.GetKeyDown(KeyCode.C)) {
selection = 2;
}
if(Input.GetKeyDown(KeyCode.D)) { // Set it back to forwards
selection = 0;
}
}
// The camera detection / movement
void LateUpdate() {
// Camera Movement
vector3 movement = CameraGoalPosition[selection] - transform.position;
movement = movement / MovementDividor;
transform.position += movement;
// Camera Look At
vector3 movementA = CameraGoalLookAt[selection] - CameraCurrentLookAt;
movementA = movementA / LookAtDividor;
CameraCurrentLookAt += movementA;
transform.lookat(CameraCurrentLookAt);
// If you dont' want the camera to have a smooth panning / turning one, then just replace the camera look at code with:
// transform.lookt(CameraGoalLookAt);
}
Hopefully this works, just ask any questions
SideNote: I suggest pre-setting cameracurrentlookat to the player position so the camera starts the game looking at the player
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
How to end a cinematic camera cycle 1 Answer
PS3 third person camera orbit script C# 0 Answers
I'm clueless about how to make my script change its position based on how close it is to the wall.. 0 Answers
Changing TPP view to a FPP view after picking up object 1 Answer