- Home /
Push an Object with a vector created from two points.
Hi, i wanted to push my Player in the direction of a vector created with the coordinate of mouseDown and mouseUp. I know, by reading in other questions, that to get a direction vector with those two coordinate i just have to create a Vector2 direction = mouseDownPosition - mouseUpPosition by using the designated void functions, but i seem to get the forces of the push wrong. if i have a fixed value pushForce, what RigidBody2D function should i use to make it seem like a "catapult-y" push? if i just use GetComponent.AddForce(direction * pushForce) the character moves but with almost no "x" force and too much "y" force. Here's what happens on my OnMouseUp() function:
public float pushForce = 10.0f;
private void OnMouseUp()
{
mouseUp = Input.mousePosition;
Vector2 pushCharacter = mouseDown - mouseUp;
GetComponent<Rigidbody2D>().AddForce(pushCharacter * pushForce);
}
i know pushForce could seem little but it's just to make you see what kind of value i use, i tried with every kind of number but the problem it's always the same. for mouseDown i used the same method i used for mouseUp.
the way you create the vector is towards mouse down. in addition, mouse positions are in screenspace, which can just usually does not match what the camera sees. transform the points into world space and calculate their force there. and then there's a version of AddForce with a second parameter where you can define how and if with mass considered you want to fire it off.
How do i transform those points into world space?
but then again, i read that it is for 3D games but in 2D ones the points are already in screen space
Your answer
Follow this Question
Related Questions
Rigidbody2D.Velocity = Vector2 not changing X Position 1 Answer
toss up game object into air with freefall physics 1 Answer
How do I do rb.AddForce but in 2D? 1 Answer
How do I make it so the player cant spam the dash 1 Answer
How would you check if an object is closer to the bottom side or top side of another object 1 Answer