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 mimfangjj22 · Mar 20, 2014 at 02:54 AM · javascriptcharacter movement

JavaScript not working

Hi, me and a friend are making a game for a school project, and we've run into some difficulties. We've tried scripting the character movement on our own and none of our scripts seem to work. We've also tried many other scripts by other people and none of them work either. Here's an example of one of our scripts:

 var speed : float = 6.0;
 var jumpSpeed : float = 8.0;
 var gravity : float = 20.0;
 private var moveDirection : Vector3 = Vector3.zero;
 function Update() {
     var controller : CharacterController = GetComponent(CharacterController);
     if (controller.isGrounded) {

         moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
                                 Input.GetAxis("Vertical"));
         moveDirection = transform.TransformDirection(moveDirection);
         moveDirection *= speed;
         
         if (Input.GetButton ("Jump")) {
             moveDirection.y = jumpSpeed;
         }
     }
 moveDirection.y -= gravity * Time.deltaTime;
     
     controller.Move(moveDirection * Time.deltaTime);
 }
Comment
Add comment · Show 7
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 Nanobrain · Mar 20, 2014 at 03:52 PM 0
Share

Have you attached the script to the game object in the Unity editor?

avatar image mimfangjj22 · Mar 23, 2014 at 10:41 PM 0
Share

Yes, we have attached it and it still doesn't work.

avatar image getyour411 · Mar 23, 2014 at 10:51 PM 0
Share

First, you should not put the reference to CharacterController in Update as it will have to figure that every frame. Is your terain a standard Unity one? If you are doing something special with the terrain then isGrounded might not ever be true.

If this was C# you would have a bunch of error about float/doubles; don't know if it matters for JS but add an "f" to your numbers at the top like 6.0f, 8.0f, etc

And as Nanobrain suggests, you have this script attached to your Player object with also has the CharacterController component attached/enabled at the same level in its hierarchy? I would expect you'd get NullRef errors if that wasn't the case but really don't know JS...

avatar image Lo0NuhtiK · Mar 23, 2014 at 11:54 PM 0
Share

UnityScript/JS doesn't care if you put an "f" at the end of float values.

avatar image Bunny83 · Mar 24, 2014 at 12:03 AM 1
Share

Is it so hard to describe what "doesn't work"? Do you get any errors at runtime or even at compile-time? Are you sure you don't set the speed variable in the inspector to 0 or something? Are you sure your "Horizontal" and "Vertical" axis are defined in the input manager?

Without a proper analysis and description of your problem you can't expect a solution from our side. Have you tried to Debug.Log some of the values inside Update?

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I'm trying to get an object to move using javascript however it won't move, can someone help? 1 Answer

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Alternative to using transform.translate and transform.position for moving objects exact values? 1 Answer

Cant Get Character To Move 1 Answer


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