- Home /
Question by
johnsmith114 · Jul 23, 2014 at 09:45 PM ·
addforcemovement scriptmouseposition
Moving player to the opposite direction where the mouse is
Hello, I've been trying to make it so that my player gets propelled to the opposite direction to where the user makes a mouse click. The following code was applied to the player. I can't get it to work even though the DrawLine debug works and the Debug.log for 2DForceVector works too.
Hopefully you can help me.
Thanks in advance.
var movementMaxVelocity : float = 10;
var jumpSpeed : float = 3;
var movementForce : float = 10;
var mouseVectorPosition : Vector2;
var playerVectorPosition : Vector2;
var 2DForceVector : Vector2;
function Update () {
mouseVectorPosition= Camera.main.ScreenToWorldPoint(Input.mousePosition);
playerVectorPosition = transform.position;
2DForceVector = (mouseVectorPosition - playerVectorPosition).normalized;
rigidbody2D.AddForce(2DForceVector * movementForce * -1);
}
Comment