- Home /
Question by
Pantsuman · Apr 28, 2017 at 05:49 AM ·
velocityscripting beginnermovement script
Object flickers back and forth if it reaches mouseposition.
Hey so i made a script to move the object it is attached to to the position of my mouse. It works so far, but when it reaches my mouseposition, the object jumps back and forth and i have no clue why.
This is my code. Maybe someone can find my stupid mistake.
private Vector2 mousePosition;
private Rigidbody2D myBody;
private Vector2 direction;
public float speed;
void Start ()
{
myBody = GetComponent<Rigidbody2D>();
}
void Update ()
{
movePlayer();
}
void movePlayer()
{
mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
direction = mousePosition - myBody.position;
direction.Normalize();
myBody.velocity = direction;
}
Comment
Your answer
Follow this Question
Related Questions
[SOLVED] How to still move the character but with different keys? 0 Answers
Velocity powered rigidbody on a moving platform without parenting. 3 Answers
Proper movement code for calculating leading shoots 1 Answer
Unity 2D movement and rotate sprite to the direction it is moving in 1 Answer
How to code a burst of speed? 1 Answer