Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by FusionSausage · Sep 28, 2013 at 08:48 PM · c#cameraheighty-axisfly

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FusionSausage · Sep 28, 2013 at 09:48 PM 0
Share

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.

avatar image FusionSausage · Sep 29, 2013 at 07:29 AM 0
Share

@BerggreenD$$anonymous$$ forgot to tag ya :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Dynamic Camera Height Depending On Player / Terrain Height 1 Answer

Multiple Cars not working 1 Answer

Unity not dividing correctly C# camera pixel width & height 1 Answer

Distribute terrain in zones 3 Answers

how do i make first person character rotate left and right along with camera? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges