Search Unity

  1. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  2. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Participate with students all over the world and build projects to teach people. Join now!
    Dismiss Notice
  5. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Help with vibrating objects upon collision.

Discussion in '2D' started by Cynicalsocks, Jul 11, 2018.

  1. Cynicalsocks

    Cynicalsocks

    Joined:
    Jul 11, 2018
    Posts:
    3
    I'm trying to move an object toward the position of my mouse when left clicked, however when the object collides with other objects it starts vibrating. I imagine the problem is the object is being teleported into the other causing it to push back but I don't know how to fix it. Please and thank you.

    Here is the script i'm working with currently.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MouseMove2D : MonoBehaviour {
    5.  
    6.     private Vector3 mousePosition;
    7.     public float moveSpeed = 0.1f;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.  
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     private void Update()
    16.     {
    17.         if(Input.GetMouseButton(0))
    18.         {
    19.             mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    20.             mousePosition.z = transform.position.z;
    21.             transform.position = Vector3.MoveTowards(transform.position, mousePosition, moveSpeed * Time.deltaTime);
    22.         }
    23.     }
    24. }
     
  2. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,319
    You need to not allow the object to pass into the other objects somehow. This can get complex but one way would be to ray cast some distance from the object and detect intersections with other objects, and don't allow the object to move forward if a ray is hit.
     
  3. jeffreyschoch

    jeffreyschoch

    Joined:
    Jan 21, 2015
    Posts:
    2,309
    You have to let the physics system do the work.

    Currently you're setting position on the Transform, which is effectively teleporting the object to the location. Then when the FixedUpdate happens, the physics system separates them. That cycle repeats and you get the vibration.

    You need to move your object using the physics system. So you affect the Rigidbody2D, and the Rigidbody2D will affect the Transform for you. You could be using AddForce, or probably MovePosition is what you want as an alternative to setting position directly using the mouse location.

    Try this:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class MouseMove2D : MonoBehaviour
    4. {
    5.     public float moveSpeed = 0.1f;
    6.  
    7.     private Vector3 mousePosition;
    8.     private Rigidbody2D _rigidbody;
    9.  
    10.     private void Awake()
    11.     {
    12.         _rigidbody = GetComponent<Rigidbody2D>();
    13.     }
    14.  
    15.     private void Update()
    16.     {
    17.         if (Input.GetMouseButton(0))
    18.         {
    19.             mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    20.             mousePosition.z = transform.position.z;
    21.  
    22.             Vector3 targetPosition = Vector3.MoveTowards(transform.position, mousePosition, moveSpeed * Time.deltaTime);
    23.  
    24.             _rigidbody.MovePosition(targetPosition);
    25.         }
    26.     }
    27. }
    You may also want to use Continuous Collision mode on the rigidbody for a more costly, but higher accuracy physics simulation.
     
    MD_Reptile and Cynicalsocks like this.
  4. Cynicalsocks

    Cynicalsocks

    Joined:
    Jul 11, 2018
    Posts:
    3

    Thanks a bunch! But now there's another problem.
    When multiple objects collide there's no vibration, but when the mouse button is released they begin to drift away. How can i make them stay still? also how do i make the objects come to a stop gradually instead of them stopping right away?

    Edit: I fixed the drifting by changing the linear and angular drag to 50.
     
    Last edited: Jul 11, 2018
    jeffreyschoch likes this.