• 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 RafaelCN · Oct 30, 2013 at 06:16 AM · movementvector2vectordiagonal

Diagonal Movement

Hello guys, I'm with one doubt, related to the diagonal movement, I've searched for questions like t$$anonymous$$s, but the diagonal script movement is already made, so as a beginner I have a doubt about how can i do the diagonal movement, I'm using a script like t$$anonymous$$s to make the movement of anyt$$anonymous$$ng:

 void Update() {
     if(Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) {  
     transform.Translate(Vector3.forward*SpeedZ);      
     }  
     else if(Input.GetKey(KeyCode.S)||Input.GetKey(KeyCode.DownArrow)) {  
     transform.Translate(Vector3.back*SpeedZ);  
     }
     else if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) {  
     transform.Translate(Vector3.left*SpeedX);  
     }    
     else if(Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) {    
     transform.Translate(Vector3.right*SpeedX);  
     }  
 }

So the diagonal movement is described by the drawing that I've made

alt text

The Cartesian plane that I've made, representing the vectors of the movement, and the vectors of the diagonal movement, so my question is, how can I do it? I've thought in divide by e.g the vectors W by D to ac$$anonymous$$eve the diagonal movement forward right, somet$$anonymous$$ng like that:

 Vector2 diagonal = W_Movement/D_Movement; 

The W_Movement is a Vector, so I don't know if t$$anonymous$$s is right, I want to understand how it works in the Cartesian plane, and if my propose it's right!

diagonal movement.png (10.4 kB)
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

1 Reply

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

Answer by robertbu · Oct 30, 2013 at 06:49 AM

The way to get a diagonal is to add the two vectors together. You will want to normalize the result. If you give up on independent speed for x and z, you can rework you code as follows:

 var speed = 2.0;
 
 function Update() {
     var v3 : Vector3;
     
 if(Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) {  
     v3 += Vector3.forward;  
     }  
     if(Input.GetKey(KeyCode.S)||Input.GetKey(KeyCode.DownArrow)) {  
     v3 += Vector3.back;  
     }
     if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) {  
     v3 += Vector3.left;  
     }    
     else if(Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) {    
     v3 += Vector3.right; 
     }  
     
     transform.Translate(speed * v3.normalized * Time.deltaTime);    
 }

FYI: t$$anonymous$$s code can be written much simpler with Input.GetAxis(). The 'Horizontal' and 'Vertical' axes are already setup and include the keys you are using. Example:

 var speed = 2.0;
 
 function Update() {
     var v3 = Vector3(Input.GetAxisRaw("Horizontal"), 0.0, Input.GetAxisRaw("Vertical"));
     transform.Translate(speed * v3.normalized * Time.deltaTime);    
 }

Note if you replace 'GetAxisRaw' with 'GetAxis' in the two places it is used, your object will have a bit of momentum carry through.

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 RafaelCN · Oct 30, 2013 at 11:07 AM 0
Share

Thanks for the answer, really gives a great explain to me :D

avatar image vikiridi · Aug 03, 2018 at 07:12 AM 0
Share

For any beginners who are reading this in 2018

var v3 = new Vector3(Input.GetAxisRaw("Horizontal"), 0.0, Input.GetAxisRaw("Vertical"));

that might confuse some people so i thought i'd comment this

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

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

How can I make an object move up on collision? 1 Answer

Mecanim character wobbles when changing camera relative direction. 0 Answers

2 questions (movement speed + terrain.) 0 Answers

Player movement 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