• 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 ariellempek · Jan 25, 2022 at 06:17 PM · collisionnpcjitteringshakingvibrate

NPC shakes / vibrate / jitter after pushed by a player

Hi, can someone help me how to solve the problem is that the npc shakes after pushed by player?

Scenario: The NPC reaches the designated point and then turns in the designated direction to be able to perform further actions. Unfortunately, when the player pushes the NPC, a NPC starts to shake.

Below I am attaching a video of the described problem.

https://youtu.be/scq88LGEd8k

Below I am attaching the code responsible for the NPC movement:

 public class Npc : MonoBehaviour
 {
     [SerializeField] Rigidbody2D rb;
     [SerializeField] float speed = 3f;
     [SerializeField] Transform circle;
     [SerializeField] Collider2D circleCollider;
 
     private void FixedUpdate()
     {
         if ( !circleCollider.bounds.Contains(transform.position) )
         {
             Vector2 target = (Vector2)circle.position - rb.position;
             float angle = Mathf.Atan2(target.y, target.x) * Mathf.Rad2Deg;
             rb.rotation = angle;
             rb.MovePosition(rb.position + target.normalized * speed * Time.deltaTime);
         }
         else
         {
             rb.rotation = 90f;
         }
         Debug.Log("contain: " + circleCollider.bounds.Contains(transform.position));
     }
 }



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

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by kylecat6330 · Jan 27, 2022 at 02:42 AM

When the player pushes the NPC it gains velocity and it continues moving in the direction is was pushed. Once it is pushed out of the circle rb.MovePosition() sends it back into the circle. However the velocity is still the same and it just moves back out of the circle and the cycle repeats. Every time the NPC enters and exits the circle its rotation is changed making it shake.

You can fix this by giving the NPC rigidbody some linear drag. That will make velocity decrease over time. You could also just set the velocity to zero whenever it is pushed out of the circle by putting in "rb.velocity = new Vector2(0, 0);", but that will probably mean that the player won't be able to push the NPC at all.

Comment
ariellempek

People who like this

1 Show 1 · 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 ariellempek · Jan 29, 2022 at 11:18 AM 0
Share

Thank you for your explanation. It really helped me to solve this problem. Below is the code containing the unwanted vibration reduction.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Npc : MonoBehaviour
 {
     [SerializeField] Rigidbody2D rb;
     [SerializeField] float speed = 3f;
     [SerializeField] Transform circle;
     [SerializeField] Collider2D circleCollider;
 
     bool state = false;
     float timeBetweenStateChanging = 0;
 
     private void FixedUpdate()
     {
         if ( !circleCollider.bounds.Contains(transform.position) )
         {
             state = true;
             Vector2 target = (Vector2)circle.position - rb.position;
             float angle = Mathf.Atan2(target.y, target.x) * Mathf.Rad2Deg;
             rb.rotation = angle;
             rb.MovePosition(rb.position + target.normalized * speed * Time.deltaTime);
         }
         else
         {
             state = false;
             rb.rotation = 90f;
         }
 
         // If state if changing to often (unwanted NPC vibrations) reduce the velocity of NPC to prevent unwanted NPC vibrations
         if (state)
         {
             timeBetweenStateChanging += Time.deltaTime;
         }
         else
         {
             if ((timeBetweenStateChanging > 0) && (timeBetweenStateChanging < 0.1f))
             {
                 rb.velocity = new Vector2(0, 0);
             }
             timeBetweenStateChanging = 0;
         }
     }
 }
 

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

201 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

Related Questions

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

How to make a Hostage Rescue Mechanics. (Counter Strike) 0 Answers

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

when i collide with npc he keeps walking. 1 Answer

The base of every gameobject shake 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