- Home /
Drag and drop with collider
Hi all. My problem is simple. I can drag and drop an item in my scene.But the problem is that my object can't collider with others objects. I don't want my object crosses others. Thank you.
Here my code:
using UnityEngine;
using System.Collections;
public class fee : MonoBehaviour {
private Vector3 pos;
public bool vole = true ;
private Vector3 screenPos;
private Vector3 currentScreenPos;
private Vector3 currentPos;
private Vector3 lastPosition;
private Vector3 lastPos;
private Vector3 offset;
private Vector3 dir;
public float moveSpeed;
private CharacterController controller;
void Start () {
}
void Update(){
MoveUpdate();
}
void MoveUpdate(){
if(vole){
if (Application.platform == RuntimePlatform.Android){
pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, 0));
}
else{
pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
lastPos = pos;
}
}
if (Input.touchCount == 1) {
Touch f0 = Input.GetTouch(0);
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
RaycastHit hit;
if (Physics.Raycast (ray, out hit, 100)) {
//On vérifie que l'on clique bien sur la fée
if(hit.collider.tag == "Fee"){
if (f0.phase == TouchPhase.Began) {
screenPos = Camera.main.WorldToScreenPoint(transform.position);
offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(f0.position.x, f0.position.y, 0));
}else if(f0.phase == TouchPhase.Moved){
currentScreenPos = new Vector3(f0.position.x, f0.position.y,0);
currentPos = Camera.main.ScreenToWorldPoint(currentScreenPos) + offset;
transform.position = currentPos;
lastPosition = currentPos;
}
}
}
}
}
void OnMouseDrag() {
transform.position = new Vector3(pos.x,pos.y,0);
}
void OnCollisionEnter(Collision collision)
{
StartCoroutine("collisionFee");
}
/*
void OnTriggerEnter(Collider other) {
StartCoroutine("collisionFee");
}
*/
IEnumerator collisionFee() {
vole = false;
transform.position = lastPosition;
transform.position = new Vector3(lastPos.x,lastPos.y,0);
yield return new WaitForSeconds(1f);
vole = true;
}
}
Hmm...if both objects have colliders attached, doesn't Unity handle the collisions for you? Are you trying to do something more than simply keeping the objects from overlapping?
You might look into this: http://docs.unity3d.com/Documentation/$$anonymous$$anual/PlatformDependentCompilation.html to replace the Application.platform == RuntimePlatform.Android method. It's just a preference thing.
Answer by nikescar · Oct 05, 2012 at 10:36 PM
This is happening because you are transforming the position of the physics object you are moving. This results in a "teleportation" because you are overriding the physics engine's positioning of the object by assigning the position of the mouse.
If you want to use the physics engine for detection, you have to use physics forces to move the object. Look for the DragRigidBody script under Assets -> Import Package -> Scripts. It should do what you are looking for.
Answer by pognoss · Oct 06, 2012 at 06:30 AM
Seriously thank you Nikescar!! I love you! XD I look for several week. I need help on French forum but no one people helped me. Again thank you!