What I want to do is when user touch a object he can move it, and specially with multitouch finger on the screen
I tried to do this ray with mouse click too, but still have the same problem
for that, I use this code on update
public LayerMask touchInputMask;
private static List<GameObject> touchList = new List<GameObject>();
public static Dictionary<int, objectst> touchobjects = new
Dictionary<int, objectst>();
private GameObject[] touchesOld;
private RaycastHit hit;
public GUIText Count, IndexLift;
private Vector3 targetPos;
public struct objectst { public Vector3 screenPoint; public Vector3 offset;
}
void Update(){
if (nbTouches > 0)
{
//nbTouches = 5;
//print(nbTouches + " touch(es) detected");
touchesOld = new GameObject[touchList.Count];
touchList.CopyTo(touchesOld);
touchList.Clear();
for (int i = 0; i < nbTouches; i++)
{
Touch touch = Input.GetTouch(i);
//print("Touch index " + touch.fingerId + " detected at
position " + touch.position);
Ray ray = Camera.main.ScreenPointToRay(touch.position);
if (Physics.Raycast(ray, out hit,Mathf.Infinity,
touchInputMask))
{
GameObject recipient = hit.transform.gameObject;
print("#### touch hit name object "+recipient.name);
touchList.Add(recipient);
//recipient.;
if (touch.phase == TouchPhase.Began)
{
print("#### tcouh begin");
objectst tempobj = new objectst();
tempobj.screenPoint =
Camera.main.WorldToScreenPoint(recipient.transform.position);
tempobj.offset = recipient.transform.position -
Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x,
touch.position.y, tempobj.screenPoint.z));
touchobjects.Add(touch.fingerId, tempobj);
}
if (touch.phase == TouchPhase.Stationary || touch.phase
== TouchPhase.Moved)
{
print("#### tcouh stationary or moved");
Vector3 curScreenPoint = new Vector3(touch.position.x, touch.position.y,
touchobjects[touch.fingerId].screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + touchobjects[touch.fingerId].offset;
print("#### objet doit être deplacer x = "+curPosition.x+"y = "+curPosition.y);
recipient.transform.position = curPosition;
}
if (touch.phase == TouchPhase.Ended)
{
print("#### tcouh ended");
Vector3 curScreenPoint = new Vector3(touch.position.x, touch.position.y,
touchobjects[touch.fingerId].screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) - touchobjects[touch.fingerId].offset;
recipient.transform.position = curPosition;
}
}
}
}
}
EDIT
screen shot
i was able to resolve the problem like this
and of course rigidbody of hit are my object i clicked on
thanks @Eddge for your help
I am writing this answer based off of information I got from the comments.
In order for
Physics.Raycast
to work correctly, you first need aCollider
on the objects you wants yourRaycast
to collide with. If your object does not have aCollider
then yourRaycast
will not return anything(As it didn't collide with anything).Collider
andRigidbody
are not the same thing, ACollider
is generally used for collision detection, and is often times used by aRigidbody
to handle physics based collision responses. An object with aRigidbody
will not collide with aRaycast
unless that object also has aCollider
.Physics
andRigidBody
will work with only 3d Colliders.Physics2D
andRigidBody2D
is for dealing with 2d physics you cannot mix 2d Physics and 3d Physics components as they will not work with each other in Unity.LayerMask
, is a tool to determine which Collision layer you want to deal with, every object just under their name has a tag, and a layer. This is used for determining which Collision Layer you want this object on. If you check the physics settings you can determine which layer collide/trigger each other.So a solution would be to add a new Layer called TouchableObject, then assign any object you want to be "Touchable" to that layer, and give it the appropriate
Collider
(2d Collider or 3d in your code you would want a 3d collider, or convert yourPhysics.Raycast
call to aPhysics2D.Raycast
). In yourTouch
script you would then make theLayerMask
only target that layer.