• 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 /
  • Help Room /
avatar image
Question by Katomarex · Nov 30, 2016 at 07:00 PM · movement2d gametopdowncontinuouspacman

How to modify a movement step by step to continuous movement?

So I'm trying to update my current script from a movement step by step (defined by units) to a continuous movement, but I've been having some troubles with it Right now, it changes direction whenever you press any of the arrow keys and also rotates to it's desired direction, like in the following script:

         public float speed = 0.4f;
         Vector2 dest = Vector2.zero;
     
         private int direction = 0;
     
         void Start () {
             dest = transform.position;
         }
         
         void Update () {
     
             Vector2 p = Vector2.MoveTowards(transform.position, dest, speed);
             GetComponent<Rigidbody2D>().MovePosition(p);
     
             if ((Vector2)transform.position == dest) {
     
                 if (Input.GetKey(KeyCode.UpArrow) && valid(Vector2.up)) {
                     direction = 1;
                     transform.eulerAngles = new Vector3(0, 0, 0); 
                     dest = (Vector2)transform.position + new Vector2(0, 2);
                 }
                 if (Input.GetKey(KeyCode.DownArrow) && valid(Vector2.down)) {
                     direction = 3;
                     transform.eulerAngles = new Vector3(0, 0, 180);
                     dest = (Vector2)transform.position + new Vector2(0, -2);
                 }
                 if (Input.GetKey(KeyCode.LeftArrow) && valid(Vector2.left)) {
                     direction = 4;
                     transform.eulerAngles = new Vector3(0, 0, 90);
                     dest = (Vector2)transform.position + new Vector2(-2, 0);
                 }
                 if (Input.GetKey(KeyCode.RightArrow) && valid(Vector2.right)) {
                     direction = 2;
                     transform.eulerAngles = new Vector3(0, 0, -90);
                     dest = (Vector2)transform.position + new Vector2(2, 0);
                 }
             }
 

And I have the following script to detect the colliders:

 bool valid(Vector2 dir) {
         Vector2 pos = transform.position;
         RaycastHit2D hit = Physics2D.Linecast(pos + dir, pos);
         return (hit.collider == GetComponent<Collider2D>());
         //return true;
     }

And the Result is something like this: https://i.gyazo.com/8182c66af84794146515d837c0427521.gif

I tried to turn into a continous movement, but the only way I made it work + - is the following:

  void Update() {
 
         Vector2 p = Vector2.MoveTowards(transform.position, dest, speed);
         GetComponent<Rigidbody2D>().MovePosition(p);
 
             if (Input.GetKey(KeyCode.UpArrow) && valid(Vector2.up)) {
                 direction = 1;
                 transform.eulerAngles = new Vector3(0, 0, 0);
             }
             if (Input.GetKey(KeyCode.DownArrow) && valid(Vector2.down)) {
                 direction = 3;
                 transform.eulerAngles = new Vector3(0, 0, 180);
             }
             if (Input.GetKey(KeyCode.LeftArrow) && valid(Vector2.left)) {
                 direction = 4;
                 transform.eulerAngles = new Vector3(0, 0, 90);
             }
             if (Input.GetKey(KeyCode.RightArrow) && valid(Vector2.right)) {
                 direction = 2;
                 transform.eulerAngles = new Vector3(0, 0, -90);
             }
 
         
         // up
         if (direction == 1) {
             dest = (Vector2)transform.position + new Vector2(0, 2);
         }
 
         // right
         if (direction == 2) {
             dest = (Vector2)transform.position + new Vector2(2, 0);
         }
 
         // down
         if (direction == 3) {
             dest = (Vector2)transform.position + new Vector2(0, -2);
         }
 
         // left
         if (direction == 4) {
             dest = (Vector2)transform.position + new Vector2(-2, 0);
         }
     }

And the result is this: https://i.gyazo.com/61f6323b64006247a58f4abd42792416.gif

The movement there is continous, but the problem there is whenever the car is between 2 colliders, it can go against each other if it changes direction (like in the beggining of that gif) and also the car does some random spins. I know it's giving that problem cause I took this line out of it:

 if ((Vector2)transform.position == dest)

But if I leave it there, whenever the car starts, it goes against the 1st collider and can't move or make a turn, it's simply stopped.

Any help to solve this problem would be nice.

Comment

People who like this

0 Show 0
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

0 Replies

  • Sort: 

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

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

Related Questions

Adding small acceleration to 2d top down movement game? 0 Answers

How can I stop my character from walking during the attack animation? 1 Answer

2D topdown RPG shooting in facing direction 0 Answers

Stop diagonal movement 2d top down game 0 Answers

Enemy MoveTowards getting stuck on obstacles 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