Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
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
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

0 Replies

· Add your reply
  • Sort: 

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

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

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

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

I want to click/touch and move my object(2D) on Y axis at certain speed, and I want it so if the user clicks again the speed will not change until it touches a certain object(Line). 1 Answer

Unexpected Symbol 'end of file' error 1 Answer

Snake like movement in 2D space 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