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 Jake33011 · May 19, 2018 at 10:13 AM · scripting problemmovementplayershaking

My player ball is shaking when it changes position

Hey guys my question is in the title. Basically when I press play everything is OK, the ball starts to go up without shaking, but when I start swiping to change position it starts shaking.

I already tried different things like changing to FixedUpdate() or reseting the player, but it doesn't change. Btw there is no animation on the ball. Can you help me ?

Here is the script with the swipe parameters :

 public class Swipe : MonoBehaviour 
 {
 private const float DEADZONE = 100.0f;

 public static Swipe Instance { set; get; }

 private bool tap, swipeLeft, swipeRight, swipeUp, swipeDown;
 private Vector2 startTouch, swipeDelta;

 public bool Tap { get { return tap; } }
 public Vector2 SwipeDelta { get { return swipeDelta; } }
 public bool SwipeLeft { get { return swipeLeft; } }
 public bool SwipeRight { get { return swipeRight; } }
 public bool SwipeUp { get { return swipeUp; } }
 public bool SwipeDown { get { return swipeDown; } }

 private void Awake()
 {
     Instance = this;
 }

 private void Update()
 {
     // Reseting all the booleans
     tap = swipeLeft = swipeRight = swipeDown = swipeUp = false;

     #region Stadalone Inputs
     if (Input.GetMouseButtonDown(0))
     {
         tap = true;
         startTouch = Input.mousePosition;
     }
     else if (Input.GetMouseButtonUp(0))
     {
         startTouch = swipeDelta = Vector2.zero;
     }
     #endregion

     #region Mobile Inputs
     if (Input.touches.Length != 0)
     {
         if (Input.touches[0].phase == TouchPhase.Began)
         {
             tap = true;
             startTouch = Input.mousePosition;
         }
         else if (Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled)
         {
             startTouch = swipeDelta = Vector2.zero;
         }
     }
     #endregion

     // Calculate The Distance
     swipeDelta = Vector2.zero;
     if (startTouch != Vector2.zero)
     {
         if (Input.touches.Length != 0)
         {
             swipeDelta = Input.touches[0].position - startTouch;
         }
         else if (Input.GetMouseButton(0))
         {
             swipeDelta = (Vector2)Input.mousePosition - startTouch;
         }
     } 

     // Did we cross the deadzone ?
     if (swipeDelta.magnitude > DEADZONE)
     {
         // Which direction ?
         float x = swipeDelta.x;
         float y = swipeDelta.y;

         if (Mathf.Abs(x) > Mathf.Abs(y))
         {
             // Left or Right
             if (x < 0)
                 swipeLeft = true;
             else
                 swipeRight = true;
         }
         else
         {
             // Up or Down
             if (y < 0)
                 swipeDown = true;
             else
                 swipeUp = true;
         }

         startTouch = swipeDelta = Vector2.zero;
     }
 }   
 }

And this the part of the script in Update() or (FixedUpdate(), it doesn't change anything) on the player :

  // Gather the inputs in which tube we should be
     if (Swipe.Instance.SwipeLeft)
         MoveTube(false);
     if (Swipe.Instance.SwipeRight)
         MoveTube(true);


     // Calculate where we should be in the future
     Vector3 targetPosition = transform.position.y * Vector3.up;
     if (desiredTube == 0)
         targetPosition += Vector3.left * TUBE_DISTANCE;
     else if (desiredTube == 2)
         targetPosition += Vector3.right * TUBE_DISTANCE; 

     // Let's calculate our move delta
     Vector3 moveVector = Vector3.zero;
     moveVector.x = (targetPosition - transform.position).normalized.x * speed;
     moveVector.y = speed;

     // Move the ball
     controller.Move(moveVector * Time.deltaTime);

 }

 private void MoveTube(bool goingRight)
 {
     desiredTube += (goingRight) ? 1 : -1;
     desiredTube = Mathf.Clamp(desiredTube, 0, 2);
 }

Thanks.

Comment
Add comment
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

1 Reply

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

Answer by Jake33011 · May 21, 2018 at 04:53 PM

Hey guys, I found how to solve the problem. So basically I was using the same speed variable to move my ball on the x and y axis and it seems that it created some “switch problem” for unity. So I just created 2 different variables for the speed and I solved the bug like this.

Comment
Add comment · 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

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

190 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 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

Mouse Player Movement Controller 1 Answer

Rotate game object and then return to its original rotation 1 Answer

manipulating position.y in unity 2 Answers

How do I setup fixed movement between predetermined tiles? 0 Answers

My enemy character is not facing the player correctly! 0 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