• 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 Ian9921 · Jun 02, 2016 at 02:55 PM · javascriptrigidbodyvector3not workingflight

Making a flight script dependent on several variables activated by another script.

For a magic based game I am making, I need there to be a flight script which lets the player fly by pressing the space bar, but only after pressing the keys O P I. I tried doing it several ways. At this point my code looks like this:

 function FixedUpdate ()
 {
     Debug.Log ("Update Called");
     if (MainScript.flying == true)
     {
         if (Input.GetKey(KeyCode.Space))
         {
             Debug.Log ("Flight Attempted");
             var input = new Vector3 (Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical"));
             GetComponent.<Rigidbody>().AddForce(input);
             Debug.Log ("Flight Successful");
         }
     }
 }

This does nothing, although all Debug.Logs are being triggered. I do not know if there is any way for this code to work, so it may be necessary to start from scratch. I really don't care anymore so long as it works. Keep in mind that I am relatively new to programming, so try to be specific in your answers as there are a lot of features that I have no clue how to use.

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 Wolfrik_Creations · Jun 02, 2016 at 06:57 PM 0
Share

is $$anonymous$$ainScript set as a variable?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by spencer_white · Jun 02, 2016 at 04:59 PM

First, make sure the object has a rigidbody. It's pretty obvious, but sometimes, people forget it.

Try saving rigidbody to a variable, so, at the beginning of the program, write,

 public Rigidbody rb;
 //You are using JS, so you may need to save it as a var instead of Rigidbody.
 //Sorry, I don't get how JS variables work


Then, In void Start, write

 function Start() {
      rb = GetComponent<Rigidbody>();
 }

Then, drag your object into where it says Rb in the editor, like so: alt text

Hope it helped! And, Good luck on your game. Sounds like a great idea.

P.S: I don't know that my code will work. I used C# :/


rigidbody.png (1.8 kB)
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 Wolfrik_Creations · Jun 02, 2016 at 06:55 PM 0
Share
 var rb : Rigidbody;
 
 function Start(){
     rb = GetComponent.<Rigidbody>();
 }

it's not mandatory but it will speed the game up a tad bit.

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

6 People are following this question.

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

Related Questions

Applying Constant Force on Multiple Axes Via Javascript? 1 Answer

Make a object move forwards 1 Answer

Flight Script throws no errors but does nothing. 2 Answers

when i try using .SimpleMove my object jumps back to its starting position 0 Answers

Constrain rigidbody movement to spline at high speeds / sharp curves 3 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