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 taborgreat · Nov 29, 2012 at 04:55 AM · movementcontrollery axisx axis

Moving on the Y Axis (Javascript)

I have a 2D game in which the camera is angled like PacMan. The X axis movement, Left and Right, work good but I can figure out how to do up and down movement. Here is my coding:

 var health : int = 100;
 var speed : int = 400;
 
 function Start () {
 
 }
 
 function Update () {
     if(Input.GetAxis("Horizontal")){
         transform.Translate(Vector3(Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0, 0));
     }
 }

Help me figure out what to type for Up and Down. I am using Javascript.

Comment
Add comment · Show 3
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 Graham-Dunnett ♦♦ · Nov 29, 2012 at 11:48 AM 2
Share

I took the time to format your question correctly.

avatar image AlucardJay · Nov 29, 2012 at 11:54 AM 0
Share

duplicate question found here : http://answers.unity3d.com/questions/356047/moving-player-up-and-down-in-2d.html

avatar image AlucardJay · Nov 29, 2012 at 12:18 PM 0
Share

I see where you are confused, so I'll just quickly explain Vector3. This is representing the X_axis, Y_axis and Z_axis, in that order. So Vector3( 1, 2, 5 ) means

 x = 1
 y = 2
 z = 5

When you say up/down it actually depends on which way you have built your 'board'. Is it going up (vertical on the Y_axis) or is it laying flat (horizontal on the Z_axis). Now you can see :

 for X-Z : Vector3( input_X, 0, input_Y )
 for X-Y : Vector3( input_X, input_Y, 0 )

here is the unity scripting reference : http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Vector3.html

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by The-Oddler · Nov 29, 2012 at 12:05 PM

I Think this should work:

 function Update () {
     var hor = Input.GetAxis("Horizontal");
     var ver = Input.GetAxis("Vertical");
     transform.Translate(Vector3(hor * speed * Time.deltaTime, ver * speed * Time.deltaTime, 0)); // Might want to swap the Y and Z here, depends on how your camera is rotated.
 }

Note: If you use this, you'll move twice as fast if you're moving diagonally. If you don't want this, try this:

 Vector3(hor, ver).normalized * speed * Time.deltaTime

Note: If I'm not mistaking, GetAxis("Horizontal") doesn't return a value between 0 an 1 (depends on your input settings what it returns.) If you normalize you'll loose this, so your speed will probably have to be higher to make it move properly.

Hope it helps ;)

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

Answer by DeveshPandey · Nov 29, 2012 at 12:11 PM

Try this :

 var health : int = 100;
 var speed : int = 400;
 
 function Start () {
 
 }
 
 function Update () {
     if(Input.GetAxis("Horizontal")){
         transform.Translate(Vector3(Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0, 0));
     }
 
   if(Input.GetAxis("Vertical")){
         transform.Translate(0,Vector3(Input.GetAxis("Vertical") * speed * Time.deltaTime, 0));
     }
 }
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

14 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Alternative to transform.Translate? 3 Answers

click to move workig script!! but pls help with rotation!! :( 1 Answer

All Unity Engine games' controls not working 1 Answer

MMD How to export model and animations to Unity as 3rd person controller? 2 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