'touch' object after instantiation

Ask for help and post bugs.

'touch' object after instantiation

Postby ODaniel » Fri Apr 17, 2015 5:30 am

Hey community!

I'm trying to instantiate an object and move it around immediately after a single touch, is this possible? if so how?

Also what is the difference between Simple Pan Gesture vs Pan Gesture?
The documentation states:
Pan Gesture: Recognizes dragging gesture.
Simple Pan Gesture: Simple Pan gesture which only relies on the first touch.

Any response is appreciated!

Thanks!
Danny
ODaniel
 
Posts: 4
Joined: Thu Apr 16, 2015 11:53 pm

Re: 'touch' object after instantiation

Postby valyard » Fri Apr 17, 2015 10:54 am

To spawn an object in 3d space you need to first catch a touch with another object or through global events. In TouchScript all touches are "owned" by gestures on objects.
So you either could add an empty object with a collider or use a FullscreenLayer (which is essentially a fullscreen collider) or subscript to TouchManager.Instance.TouchesBegan event.

SimplePanGesture uses only first finger's position to move, PanGesture uses the centroid of all touches to move.
valyard
Site Admin
 
Posts: 435
Joined: Mon Sep 08, 2014 11:57 pm

Re: 'touch' object after instantiation

Postby ODaniel » Fri Apr 17, 2015 10:32 pm

Thanks for the reply valyard.

I managed to instantiate an object in space with a single touch, but I'm trying to control that same instantiated object with the touch that instantiated it.

For example:
Touch anywhere on screen to spawn a sphere. With the same touch (without lifting your finger) drag the sphere around in space.
Release finger and touch another space to spawn another sphere etc.

I'm using the "Input" example that spawns falling spheres but instead I need them to follow the finger that spawned them.


valyard wrote:SimplePanGesture uses only first finger's position to move, PanGesture uses the centroid of all touches to move.

Thanks! that cleared a few things up.
-dan
ODaniel
 
Posts: 4
Joined: Thu Apr 16, 2015 11:53 pm

Re: 'touch' object after instantiation

Postby ODaniel » Tue Apr 21, 2015 4:09 am

I found the solution to my problem after hours of trying to understand the libraries.

I simply used a List to keep track of all the instantiated objects with point.Id as the list's indexes and used TouchManager.Instance.TouchMoved with a for loop to equate the moving touches to the object's transform.position

I modified the InputExample.cs a bit to achieve what I did. I also disabled the ball script to prevent the spheres from falling as they get instantiated.

using TouchScript;
using UnityEngine;
using TouchScript.Gestures;
using System.Collections.Generic;

public class InputExample : MonoBehaviour
{
List<GameObject> instObjects = new List<GameObject> ();
public GameObject Prefab;

private void OnEnable()
{
if (TouchManager.Instance != null)
{
TouchManager.Instance.TouchesBegan += touchBeganHandler;
TouchManager.Instance.TouchesMoved += touchMovedHandler;
}
}
private void OnDisable()
{
if (TouchManager.Instance != null)
{
TouchManager.Instance.TouchesBegan -= touchBeganHandler;
TouchManager.Instance.TouchesMoved -= touchMovedHandler;
}
}
private GameObject spawnPrefabAt(Vector2 position)
{
GameObject newObject = Instantiate (Prefab) as GameObject;
newObject.transform.position = Camera.main.ScreenToWorldPoint (new Vector3 (position.x, position.y, 10));
return newObject;
}
private void touchBeganHandler(object sender, TouchEventArgs e)
{
foreach (var point in e.Touches){
instObjects.Add (spawnPrefabAt(point.Position));
}
}

private void touchMovedHandler(object sender, TouchEventArgs e)
{
foreach (var point in e.Touches){
instObjects[point.Id].transform.position = Camera.main.ScreenToWorldPoint (new Vector3 (point.Position.x, point.Position.y, 10));
}
}
}


any response is appreciated!
Thanks!
-Dan
ODaniel
 
Posts: 4
Joined: Thu Apr 16, 2015 11:53 pm

Re: 'touch' object after instantiation

Postby Zimbres » Wed Apr 29, 2015 9:43 pm

I've did something similar. Working fine, for now.
I've did a separated component to the object to be dragged, and placed this on his OnTouch event/method.

void OnTouch(object sender, TouchEventArgs e) {
foreach (var touch in e.Touches) {
if (touch.Id == touchId) {
Vector3 p = Camera.main.ScreenToWorldPoint(new Vector3(touch.Position.x, touch.Position.y, createDistance));
transform.position = p;
}
}

}
Zimbres
 
Posts: 2
Joined: Wed Apr 29, 2015 4:13 pm

Re: 'touch' object after instantiation

Postby LouesBrisi » Wed Sep 20, 2017 1:32 pm

Ok guys, i know this post is old, and Im kind of a begginer, but i have a question here.

Im trying to do the same thing as the OP guy, but im dont need to instantiate anything. How can I do this if the object is already in the scene? Transform the object to my finger and with the same touch, move it.
LouesBrisi
 
Posts: 3
Joined: Mon Jul 03, 2017 4:19 pm

Re: 'touch' object after instantiation

Postby valyard » Wed Sep 20, 2017 3:03 pm

Check Portal example from 9.0 package. Change the code which spawns an object to the code which moves an object to the place of a touch.
valyard
Site Admin
 
Posts: 435
Joined: Mon Sep 08, 2014 11:57 pm

Re: 'touch' object after instantiation

Postby LouesBrisi » Tue Sep 26, 2017 12:55 am

valyard wrote:Check Portal example from 9.0 package. Change the code which spawns an object to the code which moves an object to the place of a touch.


As I said, im quite a begginer, dont know if I can do it myself
LouesBrisi
 
Posts: 3
Joined: Mon Jul 03, 2017 4:19 pm

Re: 'touch' object after instantiation

Postby valyard » Tue Sep 26, 2017 1:30 am

I am sorry, you will have to find someone who'd help you with this. Do you have a programmer friend?
Unfortunately, I have a huge list of things to do these days...
valyard
Site Admin
 
Posts: 435
Joined: Mon Sep 08, 2014 11:57 pm

Re: 'touch' object after instantiation

Postby LouesBrisi » Wed Sep 27, 2017 6:21 pm

valyard wrote:I am sorry, you will have to find someone who'd help you with this. Do you have a programmer friend?
Unfortunately, I have a huge list of things to do these days...


Yeah, I realize that asking like this is a little bit to much, my bad guys..
But anyways, I managed to do it my self! I just commented some lines and it works fine now. For the long term, I'll probably need to look deeper in the code tho

I can post what the code looks like now if anyone is interested!
LouesBrisi
 
Posts: 3
Joined: Mon Jul 03, 2017 4:19 pm

Next

Return to Help and Bugs

Who is online

Users browsing this forum: Bing [Bot] and 1 guest