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 Dyson6 · Mar 03, 2011 at 02:50 AM · verticalhorizontal

Translate a game object on horizontal and vertical axis?

Ok so I'm VERY new to Unity and I'm working on my first practice game but the only thing in my way right now is that I'm having trouble making a game object move horizontally and vertically

I have a script for each one and the horizontal one works but the vertical one just makes it go even more horizontal

//horizontal    
function Update () {
        var horiz : float = Input.GetAxis("Horizontal");
 transform.Translate(Vector3(horiz,0,0));
}

And for the vertical I have

//vertical
function Update () {
        var vert : float = Input.GetAxis("Vertical");
 transform.Translate(Vector3(vert,0,0));
}

I know it's a noob question but I'm really stuck on it

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

3 Replies

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

Answer by Joshua · Mar 03, 2011 at 04:19 AM

I can tell you are extremely new to this and don't really understand what the functions you are using actually 'do'. I advice to you make good use of this website to look anything you use up.

Let me explain the basic idea here though.

The Input.GetAxis horizontal and vertical stuff is a predefined function of unity, saving you the trouble of having to do this yourself. What it does is when a positive horizontal key is pressed (right arrow or d if you use wasd) it gives a positive value and viceversa. For vertical it does the same. So this variable you defined is either positive or negative depending on the key imput.

Now put this into a Vector3(x,y,z): Vector3(horiz,0,vert) will now 'point' into the direction of the arrow key are pressing. Since you want it to move into this direction take this as the direction of your movement and multiply it by your desired speed over the desired time. Time is important here, else the speed will depend on how quick your computer is.

transform.Translate(Vector3(horiz,0,vert) speed Time.deltaTime);

Now be sure to stick var speed = 10; at the top of your function. Now you can easily alter the movementspeed.

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 Eric5h5 · Mar 03, 2011 at 02:59 AM

You're using the same Vector3 for both horizontal and vertical. You'd need to use a different axis for vertical. Also, that code is framerate-dependent, and will run at different speeds on different devices (as well as on the same device, depending on what else is going on). You need to use Time.deltaTime.

Comment
Add comment · Show 2 · 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 Dyson6 · Mar 03, 2011 at 03:16 AM 0
Share

So what do I put ins$$anonymous$$d? do I need to add a y somewhere?

And I'm not sure what you want me to replace with Time.deltaTime

avatar image Eric5h5 · Mar 03, 2011 at 05:35 AM 0
Share

@Dyson: Please see the docs for Time.deltaTime and transform.Translate.

avatar image
0

Answer by roulioz · Oct 28, 2015 at 12:55 PM

Here is the solution, you have to use both together, but remember to use a different axis like other people said for vertical and horizontal inputs.

 //horizontal and vertical
 function Update () {
         var vert : float = Input.GetAxis("Vertical");
         var horiz : float = Input.GetAxis("Horizontal");
         transform.Translate(Vector3(horiz,0,vert));
 }
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

1 Person is following this question.

avatar image

Related Questions

How to move forward automatically while limiting the horizontal and vertical movement 1 Answer

Adapting Horizontal and Vertical movement to New Input System? 0 Answers

Play animation for horizontal and vertical movement? 1 Answer

How to control "Horizontal" and "Vertical" axis using UI Buttons 0 Answers

Center GUIContent Vertically 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