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 Andreapigna · Feb 18, 2015 at 09:48 AM · javascriptflightfly

Simple Flying script (JS possibly)

Hi guys, i've a problem I've tried to create a script that allows flying for my character, it's pretty simple: press A or D: rotate around the Y axis press W: rotate around the X axis so that my character looks up (45° max) press S: rotate around the X axis so that my character looks down (-45° max) all this while my character goes straight forward in the direction of transform.forward

This is the script i've written right now:

 var speed : float = 20;
 
 var rotX : float;
     
     function Update () {
     
     CC.Move(transform.forward * Time.deltaTime * speed);
     
     rotX=transform.rotation.x;
     
     if(Input.GetKey(KeyCode.A)) {
     transform.rotation.y=transform.rotation.y - (1 * Time.deltaTime);
     }
     
     if(Input.GetKey(KeyCode.D)) {
     transform.rotation.y=transform.rotation.y + (1 * Time.deltaTime);
     }
     
     if(Input.GetKey(KeyCode.W) && rotX>-0.25) {
     transform.rotation.x=transform.rotation.x - (1 * Time.deltaTime);
     }
     
     if(Input.GetKey(KeyCode.S) && rotX<0.25) {
     transform.rotation.x=transform.rotation.x + (1 * Time.deltaTime);
     }
     
     }

But here i got a problem: the rotations after a while mess a little, also the Z axis rotation changes and that creates a lot of problems. Also, i need that my charater can rotate more than 1 time around his Y axis, but with this code it can't.

I hope someone of you can give me some adviices, and sorry if i've done some grammar errors. Thank you

Comment
Add comment · Show 1
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 AlucardJay · Feb 18, 2015 at 08:04 PM 0
Share

https://keithmaggio.wordpress.com/2011/07/01/unity-3d-code-snippet-flight-script/

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Nymisu · Feb 18, 2015 at 12:26 PM

This is because transform.rotation is the rotation in worldspace, as opposed to localspace, and operates with Quaternions which are difficult to wrap your head around unless you are a mathematician.

In short: World space rotation is regardless of your character's alignment. World X remains constant, let's call it north, whether or not your character is looking east, west, or northeast.

Use transform.localRotation.eulerAngles instead, it's exactly the rotation the editor shows you.

For preventing values from going above bounds, use Mathf.Clamp(variablehere, minimum, maximum)

Comment
Add comment · Show 1 · 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 Andreapigna · Feb 18, 2015 at 05:39 PM 0
Share

Thank you for your answer! If it works, as soon as i've tried i'll accept your answer

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Setting Scroll View Width GUILayout 1 Answer

How to add accelerometer controls to this script? 0 Answers

Blimp (airship) Script 0 Answers

fly speed , increasing speed 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 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