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 /
  • Help Room /
avatar image
0
Question by lello82na · Nov 17, 2015 at 02:26 PM · c#rigidbody2d

Why my Player GameObject keeps on mooving anyway on its own without my input?

Hi All,

Sorry for my bad english!

I'm very beginner in unity I have a minigame 2D made of just 3 Levels, in which i have My Player, that is a simple Game Object with a animated sprite, and at some moments of the game, i istantiate some animated prefabs that i connect to the Player, making the prefabs children of the player. Then, i can drag the Player it by mouse(PC) or by touch(Android/Iphone) insiede a very simple Maze o few streets. I use the rigid body connected to the player. I program everything by C#. My problem is this: At the Level 1 everything works good, it means that my player mooves when i put a dragging input by mouse or touch and stop to mooves when i stop to drag or touch. At the level 2 and 3, instead, when i stop draging or touching the player, the players keeps on mooving on it's own as if someone else was touching it. How is my player able to moove its own without my input? I don't know how to fix this bug. Please help me. Thank you very much for all the future help

The next is a part of the script attached to my Player. It's in C#

 void Update()
          {
             if (Input.touchCount > 0) 
     {
         TouchPhase touchphase= Input.GetTouch(0).phase;

         switch (touchphase)
         {
         case TouchPhase.Began:
                    animator.SetInteger("a", 1); //mooving
             break;
     
         case TouchPhase.Stationary:
             animator.SetInteger("a", 0); //idle
             break;
         case TouchPhase.Moved:    
                     animator.SetInteger("a", 1); //mooving
                      float x = Input.GetAxisRaw("Mouse X");
             float y = Input.GetAxisRaw("Mouse Y");
         Vector2 pos = new Vector2 (Input.GetTouch (0).position.x, Input.GetTouch (0).position.y);
             pos = Camera.main.ScreenToWorldPoint (pos);    
                //in the Start() function, rb2d = this.gameObject.GetComponent<Rigidbody2D> ();
             rb2d.MovePosition (pos); 
                 break;

         case TouchPhase.Ended:
             animator.SetInteger ("a", 0); //idle
                 break;
         }

}

//-------------------------------------------------------------------------------------------

 void OnMouseDrag()
     {
 
         float x = Input.GetAxisRaw ("Mouse X");
         float y = Input.GetAxisRaw ("Mouse Y");
         
         Vector2 pos = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);
         
         pos = Camera.main.ScreenToWorldPoint (pos);    
 
         rb2d.MovePosition (pos);
         
     }






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

Answer by starikcetin · Nov 17, 2015 at 05:01 PM

Because if you center your camera to player object and your character moves, your code: pos = Camera.main.ScreenToWorldPoint (pos); will always return a different value, because after your character moved, the camera moves, so the screen point starts to refer a different point on world space. This is like carrot on a stick.

To overcome the issue, save your world poisiton to a vector3 and use it for things instead of casting screen position every time. Adn only update it if required (in your case, it is touch move, i guess).

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 lello82na · Nov 17, 2015 at 07:56 PM 0
Share

hi, thank yo for your answer, as i said i'm very beginer at unity3d, and i don't know well what Camera.main.SceenToWorldPoint does. I just thought that it change the Screen Real Point in Game World Point. however the main camera is not attached to the player, but it's attached to the background. in my game the background it's just a gameobject with a png image attached to it. So i think that the camera never moove, right? Then, i would like to save the position in a vector3 but if i do it i can't use RigidBody2D.$$anonymous$$ovePosition(pos) because the input argument pos must be Vector2D.

Please can you help me posting some code? Or maybe fixing $$anonymous$$e? I can't understand well how i can do on my own because my english it's not so good and i'm very beginner in unity... so a little code would help me much. :-) Thank you so much!!!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

RigidBody2D velocity randomly drops to zero 1 Answer

How do I keep enemies (rigidbodies) from pushing each other? 1 Answer

Adding Rigidbody2d to gameobject removes it from game 0 Answers

AddForce rigidbody 2d not moving object 2 Answers

Zero drop force 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