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 CanCo · Oct 09, 2013 at 06:37 AM · 2dmovement

Cubed movement

How can I move a character in a cube-like movement that moves 1 unit when you hold w a s or d?

I have done: function Start () {

     for(var n : int; n==0;n=0) {
         
         move.x = Input.GetAxisRaw("Horizontal");
         move.y = Input.GetAxisRaw("Vertical");
         while(move == Vector3.zero) {
             
             move.x = Input.GetAxisRaw("Horizontal");
             move.y = Input.GetAxisRaw("Vertical");
             yield;
             
         }
         
         if(move.x > 0 && !Physics.Linecast(transform.position,transform.position+Vector3(1,0,0)))
             transform.Translate(1,0,0);
         
         else if(move.x < 0 && !Physics.Linecast(transform.position,transform.position+Vector3(-1,0,0)))
             transform.Translate(-1,0,0);
         
         if(move.y > 0 && !Physics.Linecast(transform.position,transform.position+Vector3(0,1,0)))
             transform.Translate(0,1,0);
         
         else if(move.y < 0 && !Physics.Linecast(transform.position,transform.position+Vector3(0,-1,0)))
             transform.Translate(0,-1,0);
         
         yield WaitForSeconds(1f/moveSpeed);
         
     }
     
 }

But I was wondering if there was another method I could do this, that doesn't require for loops.. (I have this for a 2D game using the X and Y axis)

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
2

Answer by sed · Oct 09, 2013 at 10:17 AM

Use the

 var amMoving : boolean = false;
 function Update()
 {
     direction.x = Input.GetAxisRaw("Horizontal");
     direction.y = Input.GetAxisRaw("Vertical");

     if(direction != Vector3.zero && !amMoving) Move(direction);
 }

 function Move(direction : Vector3)
 {
     amMoving = true;
     var xAxis = Vector3(direction.x, 0, 0).Normalize();
     var yAxis = Vector3(0, direction.y, 0).Normalize();

     yield StartCoroutine(LinecastAndMove(xAxis));
     yield StartCoroutine(LinecastAndMove(yAxis));

     amMoving = false;
 }

 function LinecastAndMove(direction : Vector3)
 {
     if(direction != Vector3.zero  
        && !Physics.Linecast(transform.position,transform.position+direction))
     {
         transform.Translate(direction);
         yield WaitForSeconds(1f/moveSpeed);
     }
 }


Update function is ment for things that need to execute in every frame (like checking controls for movement).

Thats basically what happens here under the hood, because you have turned your Start function into a Coroutine (which might be helpful to read about also).

I have updated the code. Here is a little generalised version of your code w/o use of 'for' for main game loop.

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 CanCo · Oct 20, 2013 at 07:49 AM 0
Share

Yes, but I cannot use yield in update which I need so you move a certain speed with the moving in a grid, I do not want it able to go in between two different grid squares.

avatar image sed · Oct 20, 2013 at 09:13 AM 0
Share

Doh! You want the diagonal movement to be possible ins$$anonymous$$d of only the orthogonal directions, right?

avatar image Hoeloe · Oct 20, 2013 at 09:34 AM 0
Share

@CanCo: Of course you can use Update. While you may not be able to use yield, you have use the Time.time, or Time.deltaTime values to keep track of time in Update, and then you can only run the movement code after a given time. It's generally not a good idea to create a coroutine in Start to act as your update loop.

Also, one more thing, if you want to make a loop that never ter$$anonymous$$ates, don't use for loops, just write: while(true). While loops are better than for loops if you're not using the parameter in the for loop.

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

17 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

Related Questions

Object Squeezing Between Tiles 0 Answers

Megaman Movement 0 Answers

Character doesn't jump repeatedly 1 Answer

Simple movement problem. Need units to stop dead on collision. 3 Answers

Making sure playermodel only moves 1 unit 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