• 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
Question by Creative_calibur · Oct 12, 2018 at 02:58 AM · 2dcollisionmovementjitteringstopping

Stop or cancel movement if the player hits wall.

hello, I'm having a pretty annoying problem that I've tried everything in my skill-set. from be it 2D ray-casting(which didn't work because i need the box collider for triggers) to collision detection even just really complex nested if statements but Im still at square one with this problem. alt text

here is how its supposed to go

1: player presses direction

2:player moves in that direction(while avoiding the circle antagonist who is paroling)

3:players collider hits the trigger on the key

4: player unlocks door

5:player reaches goal

now everything else works but when the play hits a wall it will jitters back and forth here is my code and why it does that:

 //Inspector Variables

 public bool Key = false;

 Vector3 pos;                                // For movement
 float speed = 2.0f;                         // Speed of movement

 void Start()
 {
     pos = transform.position;          // Take the initial position
 }

 void Update()
 {

     //
     if (Key == true)
     {
         this.tag = "Key";
     }
     else
     {
         this.tag = "Player";

     }

    
 }

 void FixedUpdate()
 {
 
     if (Input.GetKey(KeyCode.A) && transform.position == pos)
     {        // Left
         pos += new Vector3(-1.5f, 0, 0);
     }
     if (Input.GetKey(KeyCode.D) && transform.position == pos)
     {        // Right
         pos += new Vector3(1.5f, 0, 0);
     }
     if (Input.GetKey(KeyCode.W) && transform.position == pos)
     {        // Up
         pos += new Vector3(0, 1.5f, 0);
     }
     if (Input.GetKey(KeyCode.S) && transform.position == pos)
     {        // Down
         pos += new Vector3(0, -1.5f, 0);
     }
     transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed);

 }

as you can see i use grid based movement, and keep it at a nice steady pace when it hits a wall(tagged Wall). It will continually try to push through the wall causing that jitter. This stops all movement. sorry for asking for a simple thing i know its a little embarrassing but I'm still trying to learn code.

How can I have it realize its hitting a wall and return to the last position or just cancel out the movement command all together

thank you in advance

(I should add those white boxes aren't individual but one big sprite grid)

3bd784074bb7f62d156a9447539f4474.png (10.9 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Creative_calibur · Oct 24, 2018 at 11:01 PM

this is still a problem idk what to do

Comment

People who like this

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

Answer by GamitusLabs · Oct 24, 2018 at 11:12 PM

Are you using tiles? An array/grid?

If you have something that is holding your grid spaces just check to see if the intended move-to grid space is open before setting pos.

Second: you can wrap everything in the fixedupdate into a single if statement

  if(this.pos == pos)
     {
     Keychecks
     }
     else
     {
     moveto
     }

Comment

People who like this

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

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

253 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 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 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

Make characters go through each other 1 Answer

Smooth Collisions 2D 0 Answers

Glitching / bugging / jittering / stuttering inside wall when moving 0 Answers

Player is jittery when it collides with wall (C# 3D) 0 Answers

Projectile Ricochet Issue. Travels along the surface rather than reflecting off of it 2 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