- Home /
Object following the mouse click point?
Hey :) I have a ball as an object which can jump and can be thrown forward if I press the left mouse butten. Now I want my ball to go there where I've clicked with my cursor. Basically just adding to go left or right. But I don't know how. My script for the ball so far:
public int jump = 100;
public int jumpF = 350;
void Update () {
if (Input.GetButtonUp("Fire1"))
{
GetComponent<Rigidbody>().AddForce(jumpF, jump, 0);
jump = 100;
jumpF = 350;
}}
Any ideas how I can script my thoughts? Thanks in advance!
Answer by foxthib · Feb 11, 2016 at 05:47 PM
You can check if your ball is before or after position of your cursor :
edit :
Vector3 mouseWorld = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 dir = (transform.position - mouseWorld ).normalized;
GetComponent<Rigidbody>().AddForce(dir*Force);
Your answer
Follow this Question
Related Questions
Accurate Mouse Snap to Grid within Threshold? (Float Rounding Errors) 1 Answer
Bullet mouse control 2 Answers
How to have an object moving to one direction and rotate it whitous affecting the direction 1 Answer
Check Mouse Movement on GameObject 0 Answers
Throwing an object (applying force to it when mouse is released) 2 Answers