- Home /
Camera stay on same Y axis
Hi!
I found a camera script in the wiki that I wanted to try out, and it worked very good. It just a camera like that I wanted, although this script and almost every other camera script I've found works the same. When you press W or S the camera flys where the mouse are pointing. I want my camera to always stay at the same level but involve a rotate function. So, what I need help with is that I want to change this following script to always stay at a specific height, not fly where the mouse are positioned, where the camera is looking.
ExFly_2.cs (FlyCam Extended)
using UnityEngine;
using System.Collections;
public class ExFly_2 : MonoBehaviour
{
/*
EXTENDED FLYCAM
Desi Quintans (CowfaceGames.com), 17 August 2012.
Based on FlyThrough.js by Slin (http://wiki.unity3d.com/index.php/FlyThrough), 17 May 2011.
LICENSE
Free as in speech, and free as in beer.
FEATURES
WASD/Arrows: Movement
Q: Climb
E: Drop
Shift: Move faster
Control: Move slower
End: Toggle cursor locking to screen (you can also press Ctrl+P to toggle play mode on and off).
*/
public float cameraSensitivity = 90;
public float climbSpeed = 4;
public float normalMoveSpeed = 10;
public float slowMoveFactor = 0.25f;
public float fastMoveFactor = 3;
private float rotationX = 0.0f;
private float rotationY = 0.0f;
void Start ()
{
Screen.lockCursor = true;
}
void Update ()
{
rotationX += Input.GetAxis("Mouse X") * cameraSensitivity * Time.deltaTime;
rotationY += Input.GetAxis("Mouse Y") * cameraSensitivity * Time.deltaTime;
rotationY = Mathf.Clamp (rotationY, -90, 90);
transform.localRotation = Quaternion.AngleAxis(rotationX, Vector3.up);
transform.localRotation *= Quaternion.AngleAxis(rotationY, Vector3.left);
if (Input.GetKey (KeyCode.LeftShift) || Input.GetKey (KeyCode.RightShift))
{
transform.position += transform.forward * (normalMoveSpeed * fastMoveFactor) * Input.GetAxis("Vertical") * Time.deltaTime;
transform.position += transform.right * (normalMoveSpeed * fastMoveFactor) * Input.GetAxis("Horizontal") * Time.deltaTime;
}
else if (Input.GetKey (KeyCode.LeftControl) || Input.GetKey (KeyCode.RightControl))
{
transform.position += transform.forward * (normalMoveSpeed * slowMoveFactor) * Input.GetAxis("Vertical") * Time.deltaTime;
transform.position += transform.right * (normalMoveSpeed * slowMoveFactor) * Input.GetAxis("Horizontal") * Time.deltaTime;
}
else
{
transform.position += transform.forward * normalMoveSpeed * Input.GetAxis("Vertical") * Time.deltaTime;
transform.position += transform.right * normalMoveSpeed * Input.GetAxis("Horizontal") * Time.deltaTime;
}
if (Input.GetKey (KeyCode.Q)) {transform.position += transform.up * climbSpeed * Time.deltaTime;}
if (Input.GetKey (KeyCode.E)) {transform.position -= transform.up * climbSpeed * Time.deltaTime;}
if (Input.GetKeyDown (KeyCode.End))
{
Screen.lockCursor = (Screen.lockCursor == true) ? true : false;
}
}
}
Thank you!
Answer by BerggreenDK · Sep 28, 2013 at 09:36 PM
If I understand your "goal/idea" correct, you want some sort of hovering camera looking down or forward without loosing height when you press forward in the direction you are looking, right? like a helicopter or something looking slightly downwards?
Instead of trying to understand your script here is how I would "think" the solution to that would be.
First, you want to maintain the altitude, so Y should be static. Okay, so you make an object that can move around in a 2D world on a "zero height". Once you get that to work, parent the camera onto the object and rotate the camera a little downwards + add height to the camera position.
Once you move the ground axis (object with script controlling the movment) your camera will also move around, but up in the air, looking down. Now if you later need to add "landing" sequences for fueling or whatever, its rather easy for you to do that by changing the height of the camera + the rotation of it.
Hope this gives you something to work with. Again, I dont provide you with the script of this solution as its would require a bit more time than I have + you wouldnt learn a thing from me doing it for you.
Good luck with the project.
Ask if you have further questions to this approach.
Ok, thanks for the answer. I'm definitely gonna check this out tomorrow, because it's kind of late in my time zone... But, if I understand you correctly, I should create an object (that's invincible, right?) and make it move. Then attach a camera to it and make the rotate function so it'd rotate around the object? Or should it rotate around the whole world? I'm making a kind of RTS combined with some other elements, so that's the kind of camera I'm ai$$anonymous$$g to do :) Really good explanation over all.
Your answer
![](https://koobas.hobune.stream/wayback/20220613114958im_/https://answers.unity.com/themes/thub/images/avi.jpg)