- Home /
Swipe to move object: object sometimes flying off screen even though value is clamped
Hello,
I'm quite stuck on how to solve this problem:
When the player isn't moving, and the camera is in orthographic mode - I want the player to be able to swipe up the screen and move an object up to the top of the screen, and when it reaches the top of the screen - to fall back down to the bottom of the screen.
The problem I'm having is that occasionally when you swipe (I think when you swipe particularly fast - though it's hard to tell what triggers this) the bag flies off screen. This doesn't happen in Unity remote - but only when the game is built: though I think this is probably because the touch values seem to be calculated differently for unity remote and an android build.
It seems that the values aren't being clamped correctly - as I'm not sure why the bag disappears off screen. Does anyone know how I've set this up wrong?
If anyone has any pointers, that would be really great!
Best, Laurien
EDIT: updated script..
using UnityEngine;
using System.Collections;
public class TouchDetect : MonoBehaviour {
float speed = 0.5f;
public float radius = 3.0f;
public PerspectiveSwitchFinal persp;
Vector3 centrePt = new Vector3(0,0,0);
public bool moveToCentre = false;
public GameObject bag;
public AccelerometerMagLowPass accel;
public Vector3 bagCurrentPos;
public float bagCurrentPosFloat;
void Update()
{
if (persp.orthoOn == true)
{
centrePt.x = transform.position.x;
centrePt.y = transform.position.y;
// z never changes from zero
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
if (Input.GetTouch(0).deltaPosition.y >= 0.5)
{
speed = 0.5f;
float dragValue = (Input.GetTouch(0).position.magnitude / Input.GetTouch(0).deltaTime / 200);
Vector3 movement = new Vector3(0,0, dragValue);
Vector3 newPos = transform.position + movement * Time.deltaTime * speed;
Vector3 offset = newPos - centrePt;
//transform.position = centrePt + Vector3.ClampMagnitude(offset, radius);
transform.position = new Vector3 (Mathf.Clamp(offset.x, -2f, 2f), Mathf.Clamp(offset.y, 0f, 7f), Mathf.Clamp(offset.z, 0f, 7f));
}
else if (Input.GetTouch(0).deltaPosition.y <= -0.5)
{
speed = 0f;
moveToCentre = true;
}
else if ((Input.GetTouch(0).deltaPosition.y > -0.5) && (Input.GetTouch(0).deltaPosition.y < 0.5))
{
speed = 0f;
moveToCentre = true;
}
}
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
{
bagCurrentPosFloat = this.transform.position.z;
moveToCentre = true;
}
if (moveToCentre)
{
transform.position = Vector3.MoveTowards(transform.position, centrePt, Time.deltaTime * 10f);
if (Vector3.Distance(transform.position, centrePt) <= 0.1)
{
moveToCentre = false;
transform.position = centrePt;
}
}
Since there's no position restriction in the provided code, I'm assu$$anonymous$$g you're using colliders at the stage borders, am I correct? If this is the case, you should use the rigidbody and apply velocity to it ins$$anonymous$$d of setting the position of the object. Setting the position of a object moves it regardless of physical constraints such as collisions.
Ohh great so the position restriction isn't set up right?
I thought line 40 : transform.position = centrePt + Vector3.Clamp$$anonymous$$agnitude(offset, radius);
was doing this - am I wrong?
Answer by danilonishimura · Jun 23, 2016 at 05:02 PM
The problem is that every frame you set a new central point, thus not restricting its position since the anchor is constantly moving.
Line25: centrePt.x = transform.position.x;
Line26: centrePt.y = transform.position.y;
Ohh that's stupid of me - thanks so much!!!
I've updated the script above so the line now reads: transform.position = new Vector3 ($$anonymous$$athf.Clamp(offset.x, -2f, 2f), $$anonymous$$athf.Clamp(offset.y, 0f, 7f), $$anonymous$$athf.Clamp(offset.z, 0f, 7f));
It seems to be working great now - but just checking, is this way fine to set it up?
Also - if you convert your comment to an answer I can tick it as answered!
Sorry for the delay - I completely missed your reply!
So the transform position x, y and z are clamped in a separate script, which controls the movement of the object through the accelerometer values.
I'm a little confused that this is working fine even though you say it's position is not being restricted through the $$anonymous$$athf.clamp. Surely I'm only resetting the central point for the x and y axis (which is being clamped separately), and not the z axis: which is the only one I'm wanting to clamp through the mathf.clamp script above?
Your answer
Follow this Question
Related Questions
multi touch / swipe issues 5 Answers
Diagonal swipe issues on Android 1 Answer
Moving a 3D object with 2D swipes 2 Answers
Make Swipe Detection Longer? 0 Answers