Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Creative Inventory · Nov 10, 2015 at 10:18 PM · collisionshakingridigbody

Rigidbody on player becomes shaky on collision

I'm having problems with my character's Rigidbody. Basically I have two scripts both are which attached to the player, the first script is my player movement script which also have a Addforce bounce variable with it and the second script is a disabling script which disable my player movement script for two seconds so my bounce variable can work. As mention above my I'm having problems with the Rigidbody attach to my player. When my player collides with an object that is set to trigger, my players movement gets really weird and shaky from the direction of where the object came from, until the player collides with another box collider then it goes back to normal!!! How would I stop this from occurring? Here are all three scripts. Thank you:

Disabling script:

 using UnityEngine;

 using System.Collections;

 public class Disablingscript : MonoBehaviour {

 public Playermovement playerMovementRef;
 
 void Start ()
 {
     StartCoroutine("DisableScript");
 }
 
 IEnumerator DisableScript ()
 {
     playerMovementRef.enabled = false;
     
     yield return new WaitForSeconds(2f);
     
     playerMovementRef.enabled = true;
       }
   }

Player movement script:

  using UnityEngine;

  using System.Collections;
 
  public class Playermovement : MonoBehaviour {

  public float speed = 15f;
  private Vector3 target;
 
 
 
 void Start () {
     target = transform.position;
 }
 
 void Update () {
     if (Input.GetMouseButtonDown (0)) {
         target = Camera.main.ScreenToWorldPoint (Input.mousePosition);
         target.z = transform.position.z;
     }            
         transform.position = Vector3.MoveTowards (transform.position, target, speed * Time.deltaTime);
 }
 
 void OnTriggerEnter2D(Collider2D other) 
 {
     if (other.tag == "Bouncy object")
     
     GetComponent<Rigidbody2D>().AddForce(transform.right * 500);
      }
   }
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
0

Answer by Statement · Nov 11, 2015 at 12:03 AM

 /* somewhere */
 transform.position = Vector3.MoveTowards (transform.position, target, speed * Time.deltaTime);
   
 /* elsewhere */
 GetComponent<Rigidbody2D>().AddForce(transform.right * 500);

How would I stop this from occurring?

Don't mix modiying transform and rigidbody for motion. If your game uses physics to drive motion, move your character with forces instead. Physics engine will update rigidbodys position and then you will just overwrite the position calculated by the physics engine so your character is kinda like walking in two directions at once.

AddForce applies acceleration, meaning your object maintains a velocity (that may dissipate due to friction and drag, but still, it applies motion over several frames/seconds at least). Setting transform.position just totally ignores what physics is doing and both of them update the position in different directions.

All movement should use the rigidbody unless you know what you are doing.

 /* Awake */
 rb = GetComponent<Rigidbody2D>();
 
 /* FixedUpdate */
 // No idea if this is still valid, but it's a start.
 rb.AddForce(Vector3.MoveTowards (rb.position, target, speed * Time.deltaTime)); 
 
 /* elsewhere */
 rb.AddForce(transform.right * 500);

Adjust the code to feel right as it's no longer applying velocity but rather acceleration.

Comment
Add comment · Show 4 · 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 Creative Inventory · Nov 12, 2015 at 04:55 PM 0
Share

Thank you for replying back, I've tried out what you suggested, however my players movement is now slow. For example I have to click multiple times for my player to get there. $$anonymous$$aybe I'm doing something wrong. Here's what I wrote: using UnityEngine;

   using System.Collections;

   public class Playermovement : $$anonymous$$onoBehaviour {

  public float speed = 15f;

      private Vector3 target;

       public Rigidbody2D rb;
 
 void Start () {
     target = transform.position;
 }
  void Awake () {
     rb = GetComponent<Rigidbody2D> ();
 }
 
 void Update () {
     if (Input.Get$$anonymous$$ouseButtonDown (0)) {
         target = Camera.main.ScreenToWorldPoint (Input.mousePosition);
         target.z = transform.position.z;

         transform.position = Vector3.$$anonymous$$oveTowards (transform.position, target, speed * Time.deltaTime);
                     
     }

 }        

 void FixUpdate (){
     
     rb.AddForce (Vector3.$$anonymous$$oveTowards (rb.position, target, speed * Time.deltaTime));
     rb.AddForce(transform.right * 500);
 }

 void OnTriggerEnter2D(Collider2D other) 
 {
     if (other.tag == "Bouncy object")
         GetComponent<Rigidbody2D>().AddForce(transform.right * 500);
 }

}

avatar image Statement Creative Inventory · Nov 12, 2015 at 06:53 PM 0
Share

Slow? Well that code wouldn't move the character with physics at all because "FixUpdate" is never called (It's called "FixedUpdate", not "FixUpdate"). But you still have your code in Update setting transform.position so until you remove that, you'll still get jerky movement.

avatar image Creative Inventory Statement · Nov 12, 2015 at 07:12 PM 0
Share

Okay thanks, but what would I change transform.position too

Show more comments

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

41 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

Related Questions

Limit bouncing to x and y axis only, not z axis. 0 Answers

Copy collision object position (Or just "fly" a little bit) 0 Answers

Object sticks to the side of the collider 1 Answer

Can I detect a collision between two triggers on other objects 1 Answer

CharacterController and Instantiating an object with collider 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