- Home /
Moving to worldpoint from mouse position
This should be very straightforward so I'm not certain why this isn't working. I have a bullet type entity I instantiate and it moves towards the mouse position on Awake (it doesn't update so it doesn't follow the mouse just the initial position).
With debug.log checking I am certain that the coordinates are correct and are not changed but if my mouse is to the left of my character (and the character has turned to face that direction) the bullet instead of moving towards the mouse position flies off in a random direction (generally top of the screen) even though the ScreenToWorldPoint destination is correct. I've tried to do this with constant force, add force, add relative force, etc and all do the same thing.
This is such a simple problem and such simple code I must be missing something easy here right?
Void Awake() {
_mousePos = mainGame2DCamera.ScreenToWorldPoint(Input.mousePosition); //this is definitely correct }
void FixedUpdate() {
_localRB.AddForce(_mousePos * .5f);
}
Answer by Loius · Apr 09, 2013 at 04:55 AM
You need to pass a direction into addforce - you probably want to pass the direction which is defined by "from guy to mouse position".
"From X to Y", in vectors, is equivalent to the vector (Y-X). So:
AddForce(( _mousePos - guy.position) * multiplier );
Whoops I thought Input.mousePosition world point was already a direction. Thanks for the help.
Your answer
