Skip to main content
added 2100 characters in body
Source Link
using UnityEngine;
 using System.Collections;
public class NewBehaviourScript1 : MonoBehaviour 
{ 

bool allowDrag = true;
void OnCollisionEnter(Collision collision)
{
    allowDrag = false;
}


void OnMouseDrag()
{ 
    if(allowDrag) 
    {
    float distance = 7;
    Vector3 mousePosition = new Vector3 (Input.mousePosition.x, Input.mousePosition.y , distance);
    Vector3 objPosition = Camera.main.ScreenToWorldPoint (mousePosition);
    transform.position = objPosition;
        if ((transform.position.x <0)) 
            {
            Vector3 _tmp = transform.position; 
            _tmp.x = 0;
            transform.position = _tmp;
            }
            if ((transform.position.x > 4.5f))
            {
            Vector3 _tp = transform.position;
            _tp.x = 4.5f;
            transform.position = _tp;
            }


    }
}
void OnMouseUp()
{ 
    if ((transform.position.x > 0) && (transform.position.x < 0.375f))
        {
        Vector3 _tr = transform.position;
        _tr.x = 0;
        transform.position = _tr;
        }
    if ((transform.position.x > 0.375f) && (transform.position.x < 1.25f))
        {
        Vector3 _fs = transform.position;
        _fs.x = 0.75f;
        transform.position = _fs;
        }
    if ((transform.position.x > 1.125f) && (transform.position.x < 1.875f))
        {
        Vector3 _tr2 = transform.position;
        _tr2.x = 1.5f;
        transform.position = _tr2;
        }
    if ((transform.position.x > 1.875f) && (transform.position.x < 2.625f)) 
        {
        Vector3 _tr4 = transform.position;
        _tr4.x = 2.25f;
        transform.position = _tr4;
        }
    if ((transform.position.x > 2.625f) && (transform.position.x < 3.375f)) 
        {
        Vector3 _tr6 = transform.position;
        _tr6.x = 3;
        transform.position = _tr6;
        }
        if ((transform.position.x > 3.375f) && (transform.position.x < 4.125f)) 
        {
        Vector3 _tr7 = transform.position;
        _tr7.x = 3.75f;
        transform.position = _tr7;
        }
    if ((transform.position.x > 4.125f) && (transform.position.x < 4.875f))
        {
        Vector3 _tr8 = transform.position;
        _tr8.x = 4.5f;
        transform.position = _tr8;
        }
}
void Start ()
{       
}
void Update()
{       
}

}

using UnityEngine;
 using System.Collections;
public class NewBehaviourScript1 : MonoBehaviour 
{ 

bool allowDrag = true;
void OnCollisionEnter(Collision collision)
{
    allowDrag = false;
}


void OnMouseDrag()
{ 
    if(allowDrag) 
    {
    float distance = 7;
    Vector3 mousePosition = new Vector3 (Input.mousePosition.x, Input.mousePosition.y , distance);
    Vector3 objPosition = Camera.main.ScreenToWorldPoint (mousePosition);
    transform.position = objPosition;
        if ((transform.position.x <0)) 
            {
            Vector3 _tmp = transform.position; 
            _tmp.x = 0;
            transform.position = _tmp;
            }
            if ((transform.position.x > 4.5f))
            {
            Vector3 _tp = transform.position;
            _tp.x = 4.5f;
            transform.position = _tp;
            }


    }
}
void OnMouseUp()
{ 
    if ((transform.position.x > 0) && (transform.position.x < 0.375f))
        {
        Vector3 _tr = transform.position;
        _tr.x = 0;
        transform.position = _tr;
        }
    if ((transform.position.x > 0.375f) && (transform.position.x < 1.25f))
        {
        Vector3 _fs = transform.position;
        _fs.x = 0.75f;
        transform.position = _fs;
        }
    if ((transform.position.x > 1.125f) && (transform.position.x < 1.875f))
        {
        Vector3 _tr2 = transform.position;
        _tr2.x = 1.5f;
        transform.position = _tr2;
        }
    if ((transform.position.x > 1.875f) && (transform.position.x < 2.625f)) 
        {
        Vector3 _tr4 = transform.position;
        _tr4.x = 2.25f;
        transform.position = _tr4;
        }
    if ((transform.position.x > 2.625f) && (transform.position.x < 3.375f)) 
        {
        Vector3 _tr6 = transform.position;
        _tr6.x = 3;
        transform.position = _tr6;
        }
        if ((transform.position.x > 3.375f) && (transform.position.x < 4.125f)) 
        {
        Vector3 _tr7 = transform.position;
        _tr7.x = 3.75f;
        transform.position = _tr7;
        }
    if ((transform.position.x > 4.125f) && (transform.position.x < 4.875f))
        {
        Vector3 _tr8 = transform.position;
        _tr8.x = 4.5f;
        transform.position = _tr8;
        }
}
void Start ()
{       
}
void Update()
{       
}

}

added 129 characters in body
Source Link

I work on a 2D game and I have multiple objects that the player can control.

The objects each have a RigidBody2D and a Collider, and a method OnMouseDrag() with which I can drag the objects.

I want the dragging to stop when the objects hit each other.

I tried by using this:

rigidbody2d.velocity = Vector3.zero;
rigidbody.isKinematic = true;

But the objects do not stop.

I want exactly like this video. I tried this but it isn't work too.

https://www.youtube.com/watch?v=ZfjVR-0ZFHU&t=270s

Please help!

I work on a 2D game and I have multiple objects that the player can control.

The objects each have a RigidBody2D and a Collider, and a method OnMouseDrag() with which I can drag the objects.

I want the dragging to stop when the objects hit each other.

I tried by using this:

rigidbody2d.velocity = Vector3.zero;
rigidbody.isKinematic = true;

But the objects do not stop.

Please help!

I work on a 2D game and I have multiple objects that the player can control.

The objects each have a RigidBody2D and a Collider, and a method OnMouseDrag() with which I can drag the objects.

I want the dragging to stop when the objects hit each other.

I tried by using this:

rigidbody2d.velocity = Vector3.zero;
rigidbody.isKinematic = true;

But the objects do not stop.

I want exactly like this video. I tried this but it isn't work too.

https://www.youtube.com/watch?v=ZfjVR-0ZFHU&t=270s

Please help!

Fixed the tags (and formatted the text).
Source Link
Vaillancourt
  • 16.4k
  • 17
  • 56
  • 61

Object is Objects do not stop when collisioncolliding each other objectwhile using mouse drag

I work on a 2D game and I have multiple object we couldobjects that the player can control. Objectes

The objects each have Rigidbody2Da RigidBody2D and a Collider., and a method OnMouseDrag() with which I wrote 'OnMouseDrag()'can drag the objects. 

I want when object hits each other the dragging method isto stop. But dragging is continue anyway when the objects hit each other. 

I try 'rigidbody2d.velocity = Vector3.zero;' and ' rigidbody.isKinematic = true;' It istried by using this:

rigidbody2d.velocity = Vector3.zero;
rigidbody.isKinematic = true;

But the objects do not workstop. 

Please help. Sorry my grammar.!

Object is not stop when collision other object using mouse drag

I work on 2D game and I have multiple object we could control. Objectes have Rigidbody2D and Collider. I wrote 'OnMouseDrag()'. I want when object hits each other the dragging method is stop. But dragging is continue anyway. I try 'rigidbody2d.velocity = Vector3.zero;' and ' rigidbody.isKinematic = true;' It is not work. Please help. Sorry my grammar.

Objects do not stop when colliding each other while using mouse drag

I work on a 2D game and I have multiple objects that the player can control.

The objects each have a RigidBody2D and a Collider, and a method OnMouseDrag() with which I can drag the objects. 

I want the dragging to stop when the objects hit each other. 

I tried by using this:

rigidbody2d.velocity = Vector3.zero;
rigidbody.isKinematic = true;

But the objects do not stop. 

Please help!

Source Link
Loading