- Home /
Find a point at a distance from point on a line passing through two points
That's quite a mouthful there but it's basically it. I would like to add a force to an object in a direction of a touch passing through the center of the object. Basically bouncing a ball. If you touch its edge it will "get kicked harder" than if you touch it closer to the middle.
Currently I am using a Raycast but this doesn't really do the trick. Any ideas fellow Unitarians?
if (Input.GetMouseButtonUp(0)) {
RaycastHit hit;
if(Physics.Raycast(new Vector3(ballCollider2D.center.x,ballCollider2D.center.y,0),
new Vector3(Input.mousePosition.x,Input.mousePosition.y,0),out hit,300f))
{
Vector2 jumpPosition = new Vector2(hit.transform.position.x,hit.transform.position.y);
print("Ray: " + hit);
rigidbody2D.velocity = Vector2.zero;
rigidbody2D.AddForceAtPosition(jumpForce, jumpPosition);
}
}
Edit: OK guys, let me clarify, how would I get it to work and hit something? There is a background behind it, but it returns false
.
I think you should multiply jumpForce with distance, like the following, to simulate that effect.
(ballCollider2D.center - hit.transform.position) * jumpForce
Answer by Kavorka · Apr 24, 2014 at 09:20 PM
hit.transform.position + (ballCollider2D.center - hit.transform.position) * .5f is a point on the middle of the line connecting the two points.
hit.transform.position + (ballCollider2D.center - hit.transform.position) * .3f is 30 % away from hit.transform.position.
hit.transform.position + (ballCollider2D.center - hit.transform.position).normalized * distance is on the line, distance away from hit.transform.position.
Your answer
Follow this Question
Related Questions
Problem With Physics.Raycast 1 Answer
Debug.DrawLine problem 1 Answer
interesting raycast question 2 Answers
AddForce to a point of a GameObject, not the center 1 Answer
HOW DO I USE RAYCASTHIT 2D???? 1 Answer