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 MrMonstr · Apr 12, 2013 at 06:44 PM · javascriptmovementbeginnermovenoob

Why won't my guy move forward/back?

This is the code I got from another question.

 var speed : float = 1.0;
 var rotateSpeed : float = 3.0;
  
 function Update() {
     var controller : CharacterController = GetComponent(CharacterController);
     transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
     if (controller.isGrounded) {
         // We are grounded, so recalculate
         // move direction directly from axes
         moveDirection = Vector3(0,0,Input.GetAxis("Vertical"));
         moveDirection = transform.TransformDirection(moveDirection);
         moveDirection *= speed;
         }
     }

I feel as though I understand it now. I'm slowly learning JavaScript. But I don't understand why my person won't move forward/back at all

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by iwaldrop · Apr 12, 2013 at 07:43 PM

Where do you use moveDirection to set your position? One of the things I dislike about javascript is that it isn't always obvious what the type of a variable is, but I think you're going to want to do this:

 moveDirection = Vector3(0,0,Input.GetAxis("Vertical")) * speed;
 transform.position = moveDirection;

or this:

 transform.Translate(Vector3.forward * Input.GetAxis("Vertical")) * speed);

There are various simpler ways to write this, but this should get you moving. If neither of those solutions work, then it may be possible that your "Vertical" axis isn't setup correctly.

Comment
Add comment · Show 3 · 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 fafase · Apr 12, 2013 at 08:06 PM 0
Share

Your first example is about to set the object at position (0,0,1) for the all game. As for the second example, he is about to pass through environment. Note that you also have one parenthesis too many.

avatar image iwaldrop · Apr 12, 2013 at 08:12 PM 0
Share

Right. There was supposed to be a +transform.position in the first example; typing on a phone is hard. As for the second, well, that's what the asker was doing. Personally, I use ridgidbody.$$anonymous$$ovePosition when moving rigidbodies around.

avatar image fafase · Apr 13, 2013 at 06:59 AM 0
Share

Well, he is actually moving a character controller.

avatar image
0

Answer by donutkeith · Apr 12, 2013 at 07:50 PM

(left/right, up/down, forward/backward) left or right, up or down, forward or backward are represented by + or -1 in each case. So when you say moveDirection = Vector3(0,0,Input.GetAxis("Vertical")) you are giving it a direction of (0,0,1 or -1) This then translates to a direction relative to world space or in your case relative to the transforms local space because of this line:

 moveDirection = transform.TransformDirection(moveDirection);

Try removing that line and you might want Camera.main.transform or maybe nothing at all but I don't think you want it relative to the person walking.

Try this:

 var xAxisMove = Input.GetAxis("Horizontal");
 var zAxisMove = Input.GetAxis("Vertical");
 
 moveDirection = Vector3(xAxisMove, 0, zAxisMove) * move_speed;
 
 moveDirection = Camera.main.transform.TransformDirection(moveDirection); // <= Optional part
Comment
Add comment · 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

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

13 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

Related Questions

How to move an object along the X and Z axis with arrow keys/wasd with javascript? 1 Answer

Move Object allong an ellipsoid path 1 Answer

Movement in cycles? 0 Answers

Help With "Scripting" 1 Answer

Camera movement(I have no clue how to code) 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