Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. 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.z = transform.position.z;
    20.             transform.position = Vector3.MoveTowards(transform.position, mousePosition, moveSpeed * Time.deltaTime);
    21.         }
    22.     }
    23. }
     
  2. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,087
    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,241
    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.z = transform.position.z;
    20.  
    21.             Vector3 targetPosition = Vector3.MoveTowards(transform.position, mousePosition, moveSpeed * Time.deltaTime);
    22.  
    23.             _rigidbody.MovePosition(targetPosition);
    24.         }
    25.     }
    26. }
    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.