Attempting to apply force to object, paralell to mouse drag.
Hi there, im trying to apply force to a ball using the angle of a mouse drag, so essentially the ball always travels parallel to the mouse drag.
Like this:
I know i need to store the two vectors of the mouse down and mouse up, but im not sure how to apply force to the ball in the same direction no matter where the mouse drag is done.
Answer by lstaff · Apr 14, 2016 at 06:08 AM
void Update()
{
if (Input.GetMouseButtonDown(0)){
mousePosBefore = Input.mousePosition;
}
if (Input.GetMouseButtonUp(0))
{
mousePosAfter = Input.mousePosition;
}
mouseDrag = mousePosAfter - mousePosBefore;
}
You have your vector here. If you want only the direction you can apply this:
void AddForceToTheBall(Rigidbody rbBall)
{
rbBall.AddForce(mouseDrag.normalized*yourforce,(yourforcemode))
}
Answer by SGT_Smith · Apr 14, 2016 at 02:19 PM
@lstaff, UnityAnswers is bugging out so i cant type a comment. This works... sort of. I forgot to mention this on an Isometric game, so I'd like the click and drag on the screen to translate onto the X and Z movement of the ball. If I click and drag upwards it jumps the balls, I also get some other weird behavior.
For instance if I drag from behind the course, in the direction of the course, the ball shoots off the right side.
Your answer
Follow this Question
Related Questions
Get angle between facing direction and a point 0 Answers
change angle towards direction 1 Answer
Check if one gameobject in sight of another gameobject give wrong angle. 1 Answer
Get Direction of a vector 0 Answers
Help Detecting side of an vector 3, How to check direction and face between 2 vector3 0 Answers