Hello.
I am having weird problem with multi touching and releasing 3D objects on the iOS.
Below is an explanation, thanks if advance for the help ![]()
Short explanation:
I am trying to have few objects, that shoud start spinning when i touch / hold on them and stop when i release them. Problem is that if i multi touch few objects and release them in the same order of touching, only the first object stops, the other 2 continue spinning…
The scripts were written this way, because the game was initially made for PC/MAC and i preffer to keep the OnMouseDown / Up functions and just call them with the touch…
→ In my example camera is facing Z-Forward
I will try to explain as best as possible what is happening with a real example.
1: Create (say) 4 cubes. Move them infront camera and add BoxCollider with trigger ON…
2: Add that script to each of the cubes:
#pragma strict
// Public Variables
public var startRotateSmooth : float; // Smoothly start spinning
private var minRotateSpeed : float; // the minSpeed before it stops
public var maxRotateSpeed : float; // max speed to stop accelerating
// Private Variables
private var spinSpeed : float;
private var startSpin : boolean;
private var trans : Transform;
function Start ()
{
trans = this.transform;
startRotateSmooth = 20;
minRotateSpeed = 0.5;
maxRotateSpeed = 15;
startSpin = false;
}
function Update ()
{
if (startSpin)
{
spinSpeed += Time.deltaTime * startRotateSmooth; // Start the spin smoothly
if (spinSpeed >= maxRotateSpeed) // If the smoothStart exceed the max speed = maxRotateSpeed
{
spinSpeed = maxRotateSpeed;
}
}
else if(!startSpin)
{
spinSpeed -= Time.deltaTime * startRotateSmooth;
if (spinSpeed <= minRotateSpeed)
spinSpeed = 0;
}
trans.Rotate(Vector3.back * -spinSpeed);
}
function OnMouseDown()
{
startSpin = true;
}
function OnMouseUp()
{
startSpin = false;
}
3: Add that script to the camera:
#pragma strict
// Private variables
private var objectTouched : GameObject[]; // Array to store the touched objects to
objectTouched = new GameObject[6]; // Make the array 6 elements
function Update ()
{
// Code for OnMouseDown in the iPhone. Unquote to test.
var hit : RaycastHit;
for (var i = 0; i < Input.touchCount; ++i)
{
if (Input.GetTouch(i).phase == TouchPhase.Began)
{
// Construct a ray from the current touch coordinates
var ray = camera.ScreenPointToRay (Input.GetTouch(i).position);
if (Physics.Raycast (ray,hit))
{
objectTouched *= hit.transform.gameObject; // Store the touched object in an array*
_ objectTouched*.SendMessage(“OnMouseDown”); // Send message to the touched object to OnMouseDown / Start*_
* }*
* }*
* if (Input.GetTouch(i).phase == TouchPhase.Ended || Input.GetTouch(i).phase == TouchPhase.Canceled)*
* {*
_ objectTouched*.SendMessage(“OnMouseUp”); // Send message to the stored object to OnMouseUp / Stop*
* }
}
}
4: Start the game on iOS devide and touch few of the objects in a exact way:
→ 1: Touch one object, then addTouch second one, then addTouch third one.
→ 2: Release the first touched object, release the second touched object, release the third touched object - You shoud release in the order the object was touched.
5 - Only the first object stops, the second, 3rd are continue spinning. Attached is an image showing the way of touching if the explanation is confusing…
I am guessing that the problem is with the assignment of the objets in the array, but i can’ t find what is causing it…
[9902-multitouch_release.jpg*|9902]_
thanks :) i'm not verry familiar with the Lists, i'll examine them now. I guess if i don't limit the array to 6 elements and add to it every new touch it might become pretty big and start affecting the FPS?
– instruct9rNope. Multitouch is limited to 5 fingers. So your list will never surpass that if you Add and Remove when touches begin and end. :) Lists are great, learn about them!
– OP_tossokay but if i undertand correctly, touches are like an array 1 finger [0], 2nd finger[1] right? So if i touch one finger it will add [0] that touch, second finger[1]... then if i remove the first touch, [0] will be removed and touch [1] will go to position [0] (Thus i will remove element 0 from the List), but then if i press again with second finger it will add [1] again to the touchCount and will try to assing that touch to 1 of my List which allready holds the previously touches object.. Is that correct or the touches are not working that way? thanks
– instruct9rcool, thanks for the info. I will for sure spend some time learning data structures and how the lists \ arrays actually works. Meanwhile i realy need some example (actual help) on the problem with the script, that i have provided... thanks
– instruct9rNevermind i've manage to do it with the Buildin Arrays, by using the fingerId, instead of touchCount loop... This way i was even able to limit the number of fingers that can touch the screen at the same time by limiting the array elements :) Thanks for the info abut the Generic List btw.. pretty helpfull stuff.. Cheers :)
– instruct9r