Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by monserboy · Feb 17, 2014 at 09:29 PM · androidpositiontouchjerkyjittery

Jerky/jittery object movement

Hi

I'm working on a C# script that enables a player to change an object it's position to the left or the right, depending on which side of the screen the player is touching.

However with the following code I'm getting a lot of random jerky/jittery movement at complete random times while moving the object to the left or the right.

Things I've done so far:

  • I've tried both using FixedUpdate and Update along with trying to add Time.deltaTime or other methods of the Time class in the transform.translate code parts

  • Changed the application/game framerate to a max of 120fps

  • Enabled/disabled the rigidbody2D component

  • Interpolate, extrapolate rigidbody2D component

  • Played with the VSync settings.

Current setup:

  • Unity version: 4.3.4f1 free

  • There is one main camera that doesn't move

  • The object itself sits in another object for pivot rotating reasons (the objects rotates to one side or the other while moving along the same x axis as it would without rotation)

  • Using a Nexus 5 with Android 4.4.2 (so it must have plenty of power to generate decent framerates)

alt text Link to image: link

I think it has something to do with the time interval FixedUpdate not generating fixed times during movement of the object that causes the jittery.

C# Touch (move) script:

 using UnityEngine;
 using System.Collections;
 
 public class Touch : MonoBehaviour {
 
     public GameObject player;
     private double halfScreen;
 
     // Use this for initialization
     void Start () {
         halfScreen = Screen.width / 2.0;
     }
     
     // Update is called once per frame
     void FixedUpdate () {
         if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Stationary)
         {
             Vector2 touchPosition = Input.GetTouch(0).position;
 
             //Check if it is left or right?
             if(touchPosition.x < halfScreen){
                 player.transform.Translate(Vector3.left * 0.8f);
 
             } else if (touchPosition.x > halfScreen) {
                 player.transform.Translate(Vector3.right * 0.8f);
             }
 
         }
     }
 }
 


I'm really desperate for some help, I've been searching for the answer for a day or 2 now. I've read numerous forums but none of the solutions I found helped me out.

Please also note that I'm a new user to Unity3D

Thanks in advance for the help!

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 DajBuzi · Feb 17, 2014 at 09:53 PM 2
Share

Use :

 LateUpdate()

Late update updates after all other calculations and since you're accesing touch in Update() the system needs to calculate position at it was before and where it is now.

avatar image Nanobrain · Feb 17, 2014 at 10:00 PM 1
Share

I'm wondering if it has to do with checking input in FixedUpdate(). How about checking for input in Update() and setting a boolean variable. Then, in FixedUpdate(), check the status of the variable and apply the translation if it is true.

1 Reply

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

Answer by monserboy · Feb 18, 2014 at 06:22 PM

Combining both the comments from DajBuzi and Nanobrain did the trick. I used a LateUpdate along with a boolean check.

This is the code for future reference:

 using UnityEngine;
 using System.Collections;
 
 public class Touch : MonoBehaviour {
     
     public GameObject player;
     private double halfScreen;
     private bool touched = false;
     
     // Use this for initialization
     void Start () {
         halfScreen = Screen.width / 2.0;
     }
     
     void Update(){
         if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Stationary){
             touched = true;
         } else {
             touched = false;
         }
     }
     
     
     // Update is called once per frame
     void LateUpdate () {
         if(touched == true){
             Vector2 touchPosition = Input.GetTouch(0).position;
             
             //Check if it is left or right?
             if(touchPosition.x < halfScreen){
                 player.transform.Translate(Vector3.left * 0.08f);
                 
             } else if (touchPosition.x > halfScreen) {
                 player.transform.Translate(Vector3.right * 0.08f);
             }
             
         }
     }
 }

Thanks guys!!

Comment
Add comment · Show 1 · 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 Nanobrain · Feb 18, 2014 at 10:28 PM 1
Share

Glad you got it working :)

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

20 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

Related Questions

Moving a GUI Image to the same position as a touch 0 Answers

How can I get the current x position of touch in Android? 2 Answers

How to get TouchPosition ?? 1 Answer

How do you Draw a Line Using your Finger's Position on Android 3 Answers

how do i use touch.Position 1 Answer


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