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);
}
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).
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!!!