I’m trying to add force to a bowling ball so it moves to where the mouse clicked. This is working horribly:
Vector3 TargP = (transform.position - target).normalized;
TargP.y = 0;
gameObject.GetComponent<Rigidbody>().AddForce(TargP * 5000);
target parameter assignment code:
var mouseP = Input.mousePosition;
mouseP.z = 1;
var Target = GameObject.Find("Main Camera").GetComponent<Camera>().ScreenToWorldPoint(mouseP);
I tried this, it works better than what I’ve been trying, but not perfect. Idk, it’s just sensitive or something. Could be better.
Like, if I click center, the ball goes a little or a lot to the left. Can some one fix?
Vector3 mP = Input.mousePosition;
mP.z = GameObject.Find("Bowling Ball").transform.position.z - Camera.main.transform.position.z;
Vector3 Pos = Camera.main.ScreenToWorldPoint(mP);
Vector3 direction = (Pos - GameObject.Find("Bowling Ball").transform.position).normalized;
Comp.Throw_Ball(35000,direction,0);
I actually think I got it. I mean, I don’t think the problem is with the code. I need to do some modifying on my end.
Did you Got the solution? . I also want to know this
That code should work. The standard algorithm for finding the point between two targets is
(targ1 position - targ2 position).normalized;
I’ve known this from my past experiences with Roblox Studio.
You probably learned this in school, to understand (with pseudocode and a 2d example), the way this is done in a mathematical sense is you find the difference between the two points:
diffX = destX - sourceX
diffY = destY - sourceY
Then you calculate the length of the line between these two points
lineLength = Math.sqrt((diffX * diffX) + (diffY * diffY));
The differences divided by the length of your line give you a unit vector of direction, which you can optionally choose to adjust with a speed variable
velX = (diffX/lineLength) * speed
velY = (diffY/lineLength) * speed
If you’re using this for 3d it’s mostly the same except you introduce a 3rd variable into each step for your Z pos