By using the code below i’ve been able to create a system whereby when the player clicks the mouse on the screen, a force is added to a rigidbody that propells it from its current spawn location to where the player clicked the mouse.
if(launchEnable == 1){
transform.position = Camera.main.ScreenToWorldPoint(Vector3(curMousePosition.x, curMousePosition.y, 0));
}
if(!Input.GetButton("Fire1") launchEnable == 1){
launchEnable = 2;
var targetDir = (transform.position - spawn.transform.position).normalized;
ball.rigidbody.AddForce((targetDir * 1000), ForceMode.Acceleration);
}
The direction in which the object must travel to reach the cliked location is dictated by this line of the code:
var targetDir = (transform.position - spawn.transform.position).normalized;
However the problem i’m presented with is that the further away the point that the user clicks, in relation to the rigidbody, the more force is added.
The game im creating requires that the force to a closer point is the same as to a further point.
Is there a way to make the speed at which the rigidbody moves from its spawning location to the clicked location constant regardless of how far it must travel.
Thanks.