- Home /
Rigidbody2d.addforce in the direction of mouse problem
I'm currently using this code to addforce in the direction of the mouse:
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mouseDir = mousePos - gameObject.transform.position;
mouseDir = mouseDir.normalized;
if (Input.GetMouseButtonDown(0)) {
rb.AddForce(mouseDir * 1500);
}
It works fine however, the problem is that the force applied changes depending on how far my mouse is from the object. Is there any other way to do it so that the force is not affected by the distance from the mouse?
Answer by MelvMay · Dec 08, 2015 at 12:10 PM
First, I would ensure that you're not doing this in anything other than the FixedUpdate callback otherwise it'll be dependent upon the frame-rate. Second, you're using Vector3 and not Vector2 which Unity is implicitly converting for you however in this case, it's causing a problem as you're getting Z in your 'mouseDir'.
I would reset the Z like so:
void FixedUpdate ()
{
var mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
var mouseDir = mousePos - gameObject.transform.position;
mouseDir.z = 0.0f;
mouseDir = mouseDir.normalized;
if (Input.GetMouseButtonDown(0))
{
rb.AddForce(mouseDir * 1);
}
}
... before normalizing to remove any Z difference (assuming you're not using a 3D mouse).
With this change, it will always produce a unit-normal in the direction of the mouse as you desire and ignore any Z differences. In the very least, it'll remove Z from the normalized components. If you're seeing anything else then something else is going wrong.
Hey - this is basically correct. The only dodgyness would be if you are using a perspective camera, not an orthographic one. If that's the case then you'll need to do a little more work to get a good mousePos - simply clearing z won't cut it.
Thanks for the reply! Unfortunately, the amount of force added still changes on how far the mouse is from object being thrown. This is the full script I'm using:
bool used = false;
public Rigidbody2D rb;
Collider2D cl;
void Start () {
rb = GetComponent<Rigidbody2D>();
cl = GetComponent<Collider2D>();
rb.gravityScale = 0;
cl.shared$$anonymous$$aterial.bounciness = 0f;
}
void FixedUpdate () {
var mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
var mouseDir = mousePos - gameObject.transform.position;
mousePos.z = 0.0f;
mouseDir = mouseDir.normalized;
if (Input.Get$$anonymous$$ouseButtonDown(0) && used == false) {
cl.shared$$anonymous$$aterial.bounciness = .7f;
rb.gravityScale = 1;
rb.AddForce(mouseDir * 1500);
used = true;
}
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.R)) {
Application.LoadLevel (Application.loadedLevel);
}
}
Any problems you see with it?
You've written:
mousePos.z = 0.0f;
That actually has no effect at all, as you don't use mousePos after using it to calculate mouseDir.
Replace it with mouseDir.z = 0.0f as in the example, and it should work fine.
Oh jeez, I didn't even realize that $$anonymous$$elv$$anonymous$$ay said that. Thanks for pointing it out. Thanks so much guys!
Your answer
Follow this Question
Related Questions
Platformer2D Player sticks to platform corners 0 Answers
increasing knockback of a rigidbody as received damage increases (like super smash) 1 Answer
Adding force at an angle 2D,Adding force at an angle 1 Answer
move 2d character affected by physics with velocity and/or add force 2 Answers
Jump Height Different On Android 0 Answers