Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by laurienash · Jun 23, 2016 at 02:18 PM · c#touchswipeclamp

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;
             }
         }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image danilonishimura · Jun 23, 2016 at 03:26 PM 0
Share

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.

avatar image laurienash danilonishimura · Jun 23, 2016 at 04:44 PM 0
Share

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?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image laurienash · Jun 23, 2016 at 07:06 PM 0
Share

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!

avatar image laurienash · Jul 02, 2016 at 02:41 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

166 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Detect the swipe and add force respective to it? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges