- Home /
Need Javascript to propel an object toward mouse click
I've seen a lot of variations on this request but none of them are exactly what I need. I am new to coding so I can't just take snipets here and there. I need a javascript that will handle the following:
A physics object will move toward the mouse click location at a constant rate of speed. After the click, the object will continue on in that direction and ignore any other clicks and just collide and interact with things as the physics dictates. I was using a script that would move the object to the exact position where the mouse clicked but that wasn't right because it just stopped dead at that position instead of continuing to travel on.
Answer by $$anonymous$$ · Mar 25, 2013 at 09:13 PM
for the traveling on mouse click, you could use something like:
var hit : RaycastHit;
var ray = Camera.main.SreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, hit, Mathf.Infinity))
{
Debug.Log(hit.point);
}
In that case, hit.point would return where you clicked. To make you object move towards your clicked point, use:
ObjectToMove.position = Vector.Lerp(ObjectToMove.position, hit.point, Time.deltaTime)
If you want a complete Script, here you go :)
var Clicked : boolean = false;
var ObjectTo$$anonymous$$ove : Transform;
function Update()
{
//If the Player hits the left mouse button
if(Input.Get$$anonymous$$ouseButtonDown(0))
{
var hit : RaycastHit;
var ray = Camera.main.SreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, hit, $$anonymous$$athf.Infinity))
{
Clicked = true;
var clickPoint : Vector3 = hit.point;
}
}
if(Clicked)
{
//$$anonymous$$oves the Object towards the clicked point
ObjectTo$$anonymous$$ove.position = Vector.Lerp(ObjectTo$$anonymous$$ove.position, hit.point, Time.deltaTime)
if(Vector3.Distance(ObjectTo$$anonymous$$ove.position, clickPoint) < 0.1)
{
Clicked = false;
}
}
else
{
//Insert the rest of your script here :)
}
}
Thanks for the reply with the code.
However... 1. The object starts to move toward the mouse click but then changes its $$anonymous$$d and goes back to its original facing and heads straight forward from there. 2. After the first click, I'm not able to click a new position again. The object looks like it is trying to move but just sits still. 3. It also doesn't seem to recognize clicks from behind the object, only those out in front. 4. In another test case, just a plane for the ground and a sphere for the object to move, the sphere just sort of sinks down into the plane for a few seconds and comes to a stop.
$$anonymous$$aybe I'm doing something wrong.
By the way, here's the script I've been using. It's perfect except that I would like to use Addforce ins$$anonymous$$d so that it doesn't stop at the target click. I'm new to coding so all of my attempts at altering the code toward that end have just been bad guesses with errors.
var smooth:int; // Deter$$anonymous$$es how quickly object moves towards position
private var targetPosition:Vector3;
function Update () {
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse0))
{
var playerPlane = new Plane(Vector3.up, transform.position);
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hitdist = 0.0;
if (playerPlane.Raycast (ray, hitdist)) {
var targetPoint = ray.GetPoint(hitdist);
targetPosition = ray.GetPoint(hitdist);
var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
transform.rotation = targetRotation;
}
}
transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * smooth);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613094921im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Cannonball physics? 1 Answer
Converting button to mouse click 1 Answer
2D 360 degress platformer example needed 0 Answers
Make on object hinged to the player follow the cursor 1 Answer
2d Spaceship smooth mouse movement 0 Answers