touchCount difficulty on Android - unity3d

This could be because I've been testing on a UnityRemote, however I'm trying to restart my main scene by touching with more than one finger on an android. endCondition is a variable I set as active when the player has been destroyed. Here is what my code looks like. All I want to do is restart the scene with multi-touch. Neither of the prints logged to the console so I doubt the multiple touches are being recognized. Here is my code that is inside of Update()
if (endCondition && (Input.touchCount > 1))
{
for (int i = 0; i < Input.touchCount; i++)
{
Touch t = Input.GetTouch(i);
if (t.phase == TouchPhase.Began)
{
print("Made it here");
}
if (t.phase == TouchPhase.Ended)
{
print("Made it here");
SceneManager.LoadScene("Main");
}
}
}

This should fix it:
if (endCondition)
{
int fingersAmount = Input.touchCount;
//1 FINGER
if (fingersAmount == 1)
{
Touch t = Input.GetTouch(fingersAmount - 1);
if (t.phase == TouchPhase.Began)
{
Debug.Log("ONE FINGER");
}
if (t.phase == TouchPhase.Ended)
{
Debug.Log("ONE FINGER END");
//SceneManager.LoadScene("Main");
}
}
//2 FINGERS
else if (fingersAmount == 2)
{
Touch t = Input.GetTouch(fingersAmount - 1);
if (t.phase == TouchPhase.Began)
{
Debug.Log("TWO FINGERS");
}
if (t.phase == TouchPhase.Ended)
{
Debug.Log("TWO FINGERS END");
//SceneManager.LoadScene("Main");
}
}
//3 FINGERS
else if (fingersAmount == 3)
{
Touch t = Input.GetTouch(fingersAmount - 1);
if (t.phase == TouchPhase.Began)
{
Debug.Log("THREE FINGERS");
}
if (t.phase == TouchPhase.Ended)
{
Debug.Log("THREE FINGERS END");
//SceneManager.LoadScene("Main");
}
}
}

Related

How to make GetTouch(1) not depending on GetTouch(0) and touchCount

I'm making an app which asks to keep touch 2 things at same time, but sometimes only one.
So my problem is: GetTouch(1) depends on GetTouch(0) and touchCount.
If I release 1 time the getTouch(0) : getTouch(1) will not be called anymore.
I don't know if I'm clear so here is my code:
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
}
if (touch.phase == TouchPhase.Ended)
{
}
if (Input.touchCount == 2)
{
Touch touch_ = Input.GetTouch(1);
if (touch_.phase == TouchPhase.Began)
{
}
if (touch_.phase == TouchPhase.Ended)
{
}
}
}
How can I do to still keep the Touch(1) even without Touch(0) Pressed please ? Just keep doing my stuff during "touch_.phase == TouchPhase.Began".
Thank you in advance.
you will only be able to access Input.GetTouch(1) when the touchCount is 2 or greater. You are probably looking for some sort of state variable
bool firstPress = false;
bool secondPress = false;
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
firstPress = true;
}
if (touch.phase == TouchPhase.Ended)
{
firstPress = false;
}
if (Input.touchCount == 2)
{
Touch touch_ = Input.GetTouch(1);
if (touch_.phase == TouchPhase.Began)
{
secondPress = true;
}
if (touch_.phase == TouchPhase.Ended)
{
secondPress = false;
}
}
}
if (firstPress == true && secondPress == false)
{
...
}
if (firstPress == true && secondPress == true)
{
...
}
While the GetTouch has no guarantee that the same index returns the same touch in the next frame what you can use use the Touch.fingerIndex which is unique for each touch.
So instead of using GetTouch you can access the touches via Input.touches and then try to filter out the one for your stored fingerId e.g. like
foreach (Touch touch in Input.touches)
{
if (touch.fingerIndex == storedFingerIndex)
{
// Do something
}
}

Kinect for Windows V2

In my project I need to detect multiple gestures, I made a .gbd file with 2 continuous gestures. I can detect the continuous gesture when the .gbd file had one gesture. When there is 2 gestures I am unable to read any one gesture. Do any one know the reason? Here I post my code. I used Body basics example from sdk, and added necessary functions separately to avoid memory leakage.
//separate function to initialize database, vgb frame source and vgb frame reader
void CBodyBasics::InitializeVGB()
{
HRESULT hr;
std::wstring sDbFile = L"ContinuousGestures.gbd";
hr = CreateVisualGestureBuilderDatabaseInstanceFromFile(sDbFile.c_str(), &m_pGestureDatabase);
if (SUCCEEDED(hr))
{
OutputDebugString(L"Success DB\n");
}
else
{
OutputDebugString(L"Failure DB\n");
}
for (int i = 0; i < BODY_COUNT; i++)
{
// Source
hr = CreateVisualGestureBuilderFrameSource(m_pKinectSensor, 0, &m_pVGBFrameSource[i]);
if (SUCCEEDED(hr))
{
OutputDebugString(L"Source created\n");
}
else
{
OutputDebugString(L"Soure creation failed\n");
}
// Reader
hr = m_pVGBFrameSource[i]->OpenReader(&m_pVGBFrameReader[i]);
if (SUCCEEDED(hr))
{
OutputDebugString(L"reader opened\n");
}
else
{
OutputDebugString(L"No reader\n");
}
}
}
//function to find the available gesture count, add the available gestures to source
void CBodyBasics::CheckGestureMultiple()
{
HRESULT hr;
hr = m_pGestureDatabase->get_AvailableGesturesCount(&iGestureCount);
//gesture count
if (SUCCEEDED(hr))
{
OutputDebugString(L"gesture #\n");
}
else
{
OutputDebugString(L"0 gesture\n");
}
StringCchPrintf(smessage, _countof(smessage), L"Gesture Count %d \n", iGestureCount);
OutputDebugString(smessage);
hr = m_pGestureDatabase->get_AvailableGestures(iGestureCount, m_pgesturelist);
//gesture list
if (SUCCEEDED(hr) && m_pgesturelist != nullptr)
{
OutputDebugString(L"received available gestures\n");
GestureType mtype;
WCHAR sName[260];
for (int i = 0; i < iGestureCount; ++i)
{
hr = m_pgesturelist[i]->get_GestureType(&mtype);
//gesture type
if (SUCCEEDED(hr))
{
OutputDebugString(L"gesture type\n");
if (mtype == GestureType_Discrete)
{
OutputDebugString(L"discrete\n");
}
else if (mtype == GestureType_Continuous)
{
OutputDebugString(L"continuous\n");
}
}
else
{
OutputDebugString(L"no gesture type\n");
}
hr = m_pgesturelist[i]->get_Name(260, sName);
//gesture name
if (SUCCEEDED(hr))
{
OutputDebugString(L"gesture name\n");
StringCchPrintf(smessage, _countof(smessage), L"%s \t %d \n", sName, i);
OutputDebugString(smessage);
}
else
{
OutputDebugString(L"no name\n");
}
}
}
else
{
OutputDebugString(L"not received gestures\n");
}
for (int i = 0; i < BODY_COUNT; ++i)
{
//add gestures to the source
hr = m_pVGBFrameSource[i]->AddGestures(iGestureCount, m_pgesturelist);
if (SUCCEEDED(hr))
{
OutputDebugString(L"Gesture added\n");
}
else
{
OutputDebugString(L"not added\n");
}
}
}
//function to read the vgb frame and process if the gesture is detected
void CBodyBasics::ReadVGBFrameMultiple(int t_bodyCount)
{
HRESULT hr;
for (int i = 0; i < t_bodyCount; i++)
{
hr = m_pVGBFrameReader[i]->CalculateAndAcquireLatestFrame(&m_pVGBFrame);
if (SUCCEEDED(hr) && m_pVGBFrame != nullptr)
{
OutputDebugString(L"vgb frame\n");
BOOLEAN bGestureTracked = false;
hr = m_pVGBFrame->get_IsTrackingIdValid(&bGestureTracked);
if (SUCCEEDED(hr) && bGestureTracked)
{
OutputDebugString(L"valid gesture id and gesture tracked\n");
GestureType gesturetype;
m_pgesturelist[i]->get_GestureType(&gesturetype);
switch (gesturetype)
{
case GestureType::GestureType_Discrete:
hr = m_pVGBFrame->get_DiscreteGestureResult(m_pgesturelist[i], &m_pDiscreteResult);
if (SUCCEEDED(hr) && m_pDiscreteResult != nullptr)
{
OutputDebugString(L"discrete result\n");
BOOLEAN bDetected = false;
hr = m_pDiscreteResult->get_Detected(&bDetected);
if (SUCCEEDED(hr) && bDetected)
{
OutputDebugString(L"gesture detected\n");
StringCchPrintf(smessage, _countof(smessage), L"gesture detected %d\n", bDetected);
OutputDebugString(smessage);
float confidence = 0.0f;
hr = m_pDiscreteResult->get_Confidence(&confidence);
if (SUCCEEDED(hr) && confidence != 0.0f)
{
OutputDebugString(L"confidence is detected \t");
//if (confidence > 0.3f)
//{
StringCchPrintf(smessage, _countof(smessage), L"confidence value is %f\n", confidence);
OutputDebugString(smessage);
//}
}
else
{
OutputDebugString(L"no confidence\n");
}
}
else
{
OutputDebugString(L"gesture not detected\n");
}
}
else
{
OutputDebugString(L"no discrete result\n");
}
case GestureType::GestureType_Continuous:
hr = m_pVGBFrame->get_ContinuousGestureResult(m_pgesturelist[i], &m_pContinousResult);
if (SUCCEEDED(hr) && m_pContinousResult != nullptr)
{
OutputDebugString(L"continuous gesture\n");
WCHAR gesturename[100];
hr = m_pgesturelist[i]->get_Name(100, gesturename);
//if (gesturename == L"SwipeLeft")
//{
float progress = 0.0f;
OutputDebugString(L"get progress of continuous gesture \t");
hr = m_pContinousResult->get_Progress(&progress);
if (SUCCEEDED(hr) && progress != 0.0f)
{
StringCchPrintf(smessage, _countof(smessage), L"progress value is %f\n", progress);
OutputDebugString(smessage);
}
//}
}
else
{
OutputDebugString(L"no continuous gesture\n");
}
default:
break;
}
}
else
{
OutputDebugString(L"invalid gesture id\n");
}
}
else
{
OutputDebugString(L"no frame\n");
}
}
}
Is there wrong in my code? Do I need to modify my code? Can anyone help me?

Need help on rotating image view with animation on orientation change (4 side rotation) in android

i have a image view and i need to rotate(with IOS like animation) the image view on orientation change in android(portrait,landscape,reverse portrait and reverse landscape).
please advice
void rotateAndSet(int angle) {
if (currentAngle != angle || currentImage != currentBaseImage) { // This
// is
// to
// remove
// unnecessary
// drawing
currentAngle = angle;
currentImage = currentBaseImage;
myImg = decodeBase64(currentBaseImage);
matrix = new Matrix();
matrix.postRotate(angle);
Bitmap rotated = Bitmap.createBitmap(myImg, 0, 0, myImg.getWidth(),
myImg.getHeight(), matrix, true);
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
param.addRule(RelativeLayout.ALIGN_PARENT_TOP);
/*RotateAnimation animation = new RotateAnimation(0, 90,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
animation.setInterpolator(new LinearInterpolator());
animation.setFillAfter(true);
animation.setDuration(800);
ivFullScreen.startAnimation(animation); */
ivFullScreen.setLayoutParams(param);
ivFullScreen.setImageBitmap(rotated);
}
}
i got it
public void onSensorChanged(SensorEvent event) {
synchronized (this) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
RotateAnimation animation = null;
if (event.values[0] < 4 && event.values[0] > -4) {
if (event.values[1] > 0 && orientation != ExifInterface.ORIENTATION_ROTATE_90) {
// UP
orientation = ExifInterface.ORIENTATION_ROTATE_90;
Log.i("testme","up");
animation = getRotateAnimation(0);
degrees = 0;
} else if (event.values[1] < 0 && orientation != ExifInterface.ORIENTATION_ROTATE_270) {
// UP SIDE DOWN
Log.i("testme","up side down");
orientation = ExifInterface.ORIENTATION_ROTATE_270;
animation = getRotateAnimation(180);
degrees = 180;
}
} else if (event.values[1] < 4 && event.values[1] > -4) {
if (event.values[0] > 0 && orientation != ExifInterface.ORIENTATION_NORMAL) {
// LEFT
Log.i("testme","left");
orientation = ExifInterface.ORIENTATION_NORMAL;
animation = getRotateAnimation(-270);
degrees =-270;
} else if (event.values[0] < 0 && orientation != ExifInterface.ORIENTATION_ROTATE_180) {
// RIGHT
Log.i("testme","right");
orientation = ExifInterface.ORIENTATION_ROTATE_180;
animation = getRotateAnimation(270);
degrees = 270;
}
}
if (animation != null) {
ivFullScreen.startAnimation(animation);
}
}
}
}

Drag and drop in unity 2d

I am trying to implement drag and drop functionality for my game in unity 2d. I have multiple copies of same object in my screen and they differ only by collider name. I attached the same script to them. Here is a piece of my code
function Start () {
playerTouches = [-1, -1];
}
function resetPlayer(touchNumber: int) {
for(var i = 0; i < playerTouches.length; ++i) {
if(touchNumber == playerTouches[i]) {
playerTouches[i] = -1;
}
}
}
function getCollider(vec: Vector2) {
var ray : Ray = Camera.main.ScreenPointToRay(vec);
var hit : RaycastHit2D = Physics2D.Raycast(ray.origin, ray.direction);
if (hit) {
if (hit.collider != null) {
Debug.Log(hit.collider.name);
return hit.collider.name;
} else {
Debug.Log("is null");
return "null";
}
} else {
Debug.Log("empty");
return "";
}
return "";
}
function processTouch(touch: Touch, touchNumber: int) {
if(touch.phase == TouchPhase.Began) {
var colliderName: String = getCollider(touch.position);
if(colliderName == "Object01" && playerTouches[0] == -1) {
playerTouches[0] = touchNumber;
} else if(colliderName == "Object02" && playerTouches[1] == -1) {
playerTouches[1] = touchNumber;
}
} else if(touch.phase == TouchPhase.Moved) {
// get object and change coords
} else if(touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled) {
resetPlayer(touchNumber);
}
}
function Update() {
if(Input.touchCount > 0) {
//Debug.Log("count = " + Input.touchCount);
for(var i = 0; i < Input.touchCount; i++)
{
processTouch(Input.GetTouch(i), i);
//Debug.Log("touch : " + i + " " + Input.GetTouch(i).position);
}
}
}
For now I'm detecting on which object user touch. I need to be able to get that object and change it's position.
I also found this code snippet which allows to move rigidbody
var touchDeltaPosition: Vector2 = touch.deltaPosition;
var touchPosition: Vector2;
touchPosition.Set(touchDeltaPosition.x, touchDeltaPosition.y);
rigidbody2D.transform.position = Vector2.Lerp(transform.position, touchPosition, Time.deltaTime * spd);
but it moves all objects regardless of what object I select.
Well, you can do like this. If you have 12 copies of same object and want to move the object which selected by user. So when user Touches the object Change that GameObject tag or Name to another tag. Afterward you can use the some Conditional Statement to work with your code.
Example :
if(Input.GetMouseButtonDown(0)) {
Debug.Log("Mouse is down");
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo = new RaycastHit();
//bool hit = Physics.Raycast (Camera.main.ScreenPointToRay (Input.mousePosition), out hitInfo);
if(Physics.Raycast(ray, out hitInfo, 30)) {
Debug.Log("Hit " + hitInfo.transform.gameObject.name);
if(hitInfo.transform.gameObject.tag == "Deselected") {
//Debug.Log ("It's working! Attaching MoveCube Script to game :" + hitInfo.transform.gameObject.tag);
findObject = GameObject.FindGameObjectsWithTag("Deselected");
foreach(GameObject go in findObject) {
//go.gameObject.renderer.material.color = Color.white;
go.GetComponent<MoveCube>().enabled = false;
if(hitInfo.transform.gameObject.name.Equals(go.gameObject.name)) {
//hitInfo.transform.renderer.material.color = Color.white;
hitInfo.transform.gameObject.GetComponent<MoveCube>().enabled = true;
changeTAG = true;
} else {
hitInfo.transform.gameObject.tag = "Deselected"
}
}
playerObject = GameObject.FindGameObjectsWithTag("Player");
foreach(GameObject game in playerObject) {
count++;
if(count == 1) {
hitInfo.transform.gameObject.tag = "Player";
}
if(count >= 1) {
game.gameObject.tag = "Deselected";
game.gameObject.GetComponent<MoveCube>().enabled = false;
//game.gameObject.renderer.material.color = Color.white;
}
}
if(changeTAG) {
hitInfo.transform.gameObject.tag = "Player";
/*if (hitInfo.transform.gameObject.GetComponent<Rigidbody> ()) {
Debug.Log ("RigidBody is already added Can't add another RigidBody");
hitInfo.transform.rigidbody.WakeUp ();
} else {
hitInfo.transform.gameObject.AddComponent<Rigidbody> ().useGravity = false;
// hitInfo.transform.gameObject.GetComponent<Rigidbody> ().WakeUp ();
}*/
changeTAG = false;
} else if(!changeTAG) {
hitInfo.transform.gameObject.tag = "Deselected";
}
} else {
Debug.Log("Not Working");
}
} else {
Debug.Log("No hit");
}
Debug.Log("Mouse is down");
}
The above code is for Change the tag for selected and deselected cube. After that you can easily identify the Selected gameObject and can move it where ever you want.
You can use this code in the Update function.

Limiting JASidePanel Center panel slide

Hi I am using JASidePanels to create the left menu style but I want to do some particular changes.
The first is that I want the menu to have only 115 px wide. And for that I have found:
self.leftFixedWidth = 115;
Ok but, I can still slide the center panel to the right and see whats "hidden". Is there a way to fix its maximum position at 115 px? Just like the facebook menu?
go to JASidePanelController.m
find - (CGFloat)_correctMovement:(CGFloat)movement; method, replace it with this:
- (CGFloat)_correctMovement:(CGFloat)movement {
CGFloat position = _centerPanelRestingFrame.origin.x + movement;
if (self.state == JASidePanelCenterVisible) {
if (self.state != JASidePanelLeftVisible) {
if (position >= self.leftVisibleWidth) {
return self.leftVisibleWidth;
}
}
else if ((position > 0.0f && !self.leftPanel) || (position < 0.0f && !self.rightPanel)) {
return 0.0f;
}
} else if (self.state == JASidePanelRightVisible && !self.allowRightOverpan) {
if ((position + _centerPanelRestingFrame.size.width) < (self.rightPanelContainer.frame.size.width - self.rightVisibleWidth)) {
return 0.0f;
} else if (position > self.rightPanelContainer.frame.origin.x) {
return self.rightPanelContainer.frame.origin.x - _centerPanelRestingFrame.origin.x;
}
} else if (self.state == JASidePanelLeftVisible && !self.allowLeftOverpan) {
if (position > self.leftVisibleWidth) {
return 0.0f;
} else if (position < self.leftPanelContainer.frame.origin.x) {
return self.leftPanelContainer.frame.origin.x - _centerPanelRestingFrame.origin.x;
}
}
return movement;
}