- Home /
How do I rotate player's camera to peak around objects. (first person shooter leaning)
After about 2-3 years of doing just 3D modeling and not really getting anywhere with my games, I decided to start learning programming. I know a bit of UnityScript(Java) and even less of C#. I decided to test out my knowledge of programming by making a script called Peaking for both C# and Java as tests. I then left C# alone as I got confused so in one section of code(for Java), I typed:
if(Input.GetKeyDown(KeyCode.E)) Transform.Rotate(0, 0, 1);
The console then gave me an error saying: "An instance of type 'UnityEngine.Transform' is required to access non static member 'Rotate'.:
I then messed around a bit and came up with this script:
function Update () { if(Input.GetKeyDown(KeyCode.E)) transform.Translate(1, 0, 0);
if(Input.GetKeyUp(KeyCode.E))
transform.Translate(-1, 0, 0);
if(Input.GetKeyDown(KeyCode.Q))
transform.Translate(-1, 0, 0);
if(Input.GetKeyUp(KeyCode.Q))
transform.Translate(1, 0, 0);
}
What I'm basically trying to do is rotate the camera along the Z axis to simulate the peaking effect going around a corner instead of making the head shift to the side like I did.
It's called peeking ;) Peaking would be when the highest point of something is reached (the peak). Compare "Peak Oil".
Transform.Translate is for moving the transform. To rotate it, you have to use one of the rotation functions, such as Transform.Rotate.
It seems that this question has been asked before several times, maybe looking at some of the other threads can help you:
Answer by GameMaker_ · Jul 04, 2016 at 04:46 PM
Firstly, each movement has to be made according to the frame rate. Update is a function which is called each frame, and according to the frame rate, you will have to make your character move more or less. Usually, it will look like:
if(Input.GetKey(KeyCode.W))
transform.Translate(0, 0, Time.deltaTime * speed);
if(Input.GetKey(KeyCode.S))
transform.Translate(0, 0, -Time.deltaTime * speed);
if(Input.GetKey(KeyCode.A))
transform.Translate(Time.deltaTime * speed, 0, 0);
if(Input.GetKey(KeyCode.D))
transform.Translate(-Time.deltaTime * speed, 0, 0);
This code can be simplified, but this one is explicit.
Then to make your character to rotate, you need to go to Edit > Project settings > Input and you will see ("Mouse X" and "Mouse Y"). It enables you to write:
transform.Rotate(0, 0, Input.GetAxis("Mouse X"));
And you will be able to set the sensitivity (°/pixel) in the Input settings.
In the Input settings, you can see "Horizontal" and "Vertcial" too. It enables to simplify the translation script:
transform.Translate(Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0, Input.GetAxis("Vertical") * speed * Time.deltaTime);
Your answer
Follow this Question
Related Questions
Weird shake when I try to rotate player according to Virtual Cameras rotation. 0 Answers
rotation z of child wont work can some one help me plz 0 Answers
Get current Z rotation of transform in degrees 0 Answers
how to set z rotation to be always 0 1 Answer
How to "clamp" a y rotation in unity? 0 Answers